Skip to content

Instantly share code, notes, and snippets.

@AlexTiTanium
Last active October 6, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTiTanium/bf0abb976a944eae6bc2cb2967c0c561 to your computer and use it in GitHub Desktop.
Save AlexTiTanium/bf0abb976a944eae6bc2cb2967c0c561 to your computer and use it in GitHub Desktop.
new object allocation #jsbench #jsperf (https://jsbench.github.io/#bf0abb976a944eae6bc2cb2967c0c561) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>new object allocation #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
suite.add("var iterations = 10000000;", function () {
var iterations = 10000000;
function functionOne(a, b, c, d) {
return {
x: a.x + b.x + c.x + d.x,
y: a.y + b.y + c.y + d.y
};
};
for(var i = 0; i < iterations; i++ ){
var a = { x: Math.random(), y: Math.random() };
var b = { x: Math.random(), y: Math.random() };
var c = { x: Math.random(), y: Math.random() };
var d = { x: Math.random(), y: Math.random() };
var result = functionOne(a, b, c, d);
};
});
suite.add("var iterations = 10000000;", function () {
var iterations = 10000000;
function functionTwo(ax, bx, cx, dx, ay, by, cy, dy, resultObject) {
resultObject.x = ax + bx + cx +dx;
resultObject.y = ay + by + cy +dy;
return resultObject;
};
var resultObject = { x: null, y: null };
for(var i = 0; i < iterations; i++ ){
var ax = Math.random();
var bx = Math.random();
var cx = Math.random();
var dx = Math.random();
var ay = Math.random();
var by = Math.random();
var cy = Math.random();
var dy = Math.random();
var result = functionTwo(ax, bx, cx, dx, ay, by, cy, dy, resultObject);
};
});
suite.add("var iterations = 10000000;", function () {
var iterations = 10000000;
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.getSum = function() {
return this.x + this.y;
};
function functionThree(ax, bx, cx, dx, ay, by, cy, dy) {
return new Point(ax + bx + cx + dx, ay + by + cy + dy);
};
for(var i = 0; i < iterations; i++ ){
var ax = Math.random();
var bx = Math.random();
var cx = Math.random();
var dx = Math.random();
var ay = Math.random();
var by = Math.random();
var cy = Math.random();
var dy = Math.random();
var result = functionThree(ax, bx, cx, dx, ay, by, cy, dy);
};
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("new object allocation #jsbench #jsperf");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment