-
-
Save jaszczw/b60d1a2e540ba0224b01f330bdb7ec7c to your computer and use it in GitHub Desktop.
Sum with reference from closure, and without. (http://jsbench.github.io/#b60d1a2e540ba0224b01f330bdb7ec7c) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Sum with reference from closure, and without.</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
var array = []; | |
for (var i = 1000; i-- ;) array.push(i); | |
var sumRef = 0, sumNoref =0; | |
/* Not used only referenced */ | |
function add( n ) { | |
sumRef += n; | |
} | |
function getAdd(plus){ | |
return plus; | |
} | |
}; | |
Benchmark.prototype.teardown = function () { | |
console.log(sumNoref, sumRef) | |
}; | |
suite.add("Reverse loop, implicit comparison, variable referenced in function", function () { | |
// Reverse loop, implicit comparison, variable referenced in function | |
for ( var i = array.length; i--; ) { | |
sumRef += array[i]; | |
} | |
}); | |
suite.add("Reverse loop, implicit comparison, variable only referenced here", function () { | |
// Reverse loop, implicit comparison, variable only referenced here | |
for ( var i = array.length; i--; ) { | |
sumNoref += array[i]; | |
} | |
}); | |
suite.add("Reverse loop, implicit comparison, addition using function", function () { | |
// Reverse loop, implicit comparison, addition using function | |
for ( var i = array.length; i--; ) { | |
sumNoref += getAdd(array[i]); | |
} | |
}); | |
suite.add("Reverse loop, implicit comparison, addition using function", function () { | |
// Reverse loop, implicit comparison, addition using function | |
for ( var i = array.length; i--; ) { | |
sumRef += getAdd(array[i]); | |
} | |
}); | |
suite.add("Reverse loop, implicit comparison, addition using intermidate", function () { | |
// Reverse loop, implicit comparison, addition using intermidate | |
var newSum = 0; | |
for ( var i = array.length; i--; ) { | |
newSum += array[i]; | |
} | |
sumRef = newSum; | |
}); | |
suite.add("Reduce", function () { | |
// Reduce | |
sumRef = array.reduce(function(a,b){return a+b;}); | |
}); | |
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("Sum with reference from closure, and without."); | |
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