Skip to content

Instantly share code, notes, and snippets.

@gabsoftware
Last active August 9, 2017 08:55
Show Gist options
  • Save gabsoftware/b6aa5dcbe07a27db2a63176f3d9d1bfe to your computer and use it in GitHub Desktop.
Save gabsoftware/b6aa5dcbe07a27db2a63176f3d9d1bfe to your computer and use it in GitHub Desktop.
Finding out the fastest way to loop through an array (http://jsbench.github.io/#b6aa5dcbe07a27db2a63176f3d9d1bfe) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Finding out the fastest way to loop through an array</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;
Benchmark.prototype.setup = function () {
var arr = [];
for( var cpt = 0; cpt < 1000; cpt++ ) {
arr.push( Math.random() );
}
var someFn = function( item ) {
return item * 3 + item / 5;
}
//console.log( "benchmark started" );
};
Benchmark.prototype.teardown = function () {
//console.log( "benchmark ended" );
};
suite.add("for( var i = 0, n = arr.length; i < n; i++ ) {", function () {
for( var i = 0, n = arr.length; i < n; i++ ) {
someFn( arr[ i ] );
}
});
suite.add("for( var i = arr.length; i > -1; i-- ) {", function () {
for( var i = arr.length; i > -1; i-- ) {
someFn( arr[ i ] );
}
});
suite.add("arr.forEach( someFn );", function () {
arr.forEach( someFn );
});
suite.add("for( var e = arr.entries(), n = e.next(); ! n.done; n = e.next() ) {", function () {
for( var e = arr.entries(), n = e.next(); ! n.done; n = e.next() ) {
someFn( n.value[ 1 ] );
}
});
suite.add("var e = arr.entries(), n = e.next();", function () {
var e = arr.entries(), n = e.next();
while( ! n.done ) {
someFn( n.value[ 1 ] );
n = e.next();
}
});
suite.add("for( var i in arr ) {", function () {
for( var i in arr ) {
someFn( arr[ i ] );
}
});
suite.add("for( r of arr ) {", function () {
for( r of arr ) {
someFn( r );
}
});
suite.add("arr.map( someFn );", function () {
arr.map( someFn );
});
suite.add("arr.filter( someFn );", function () {
arr.filter( someFn );
});
suite.add("var e = arr.entries(), n;", function () {
var e = arr.entries(), n;
while( ! ( n = e.next() ).done ) {
someFn( n.value[ 1 ] );
}
});
suite.add("for( var e = arr.entries(), n; ! (n = e.next()).done; ) {", function () {
for( var e = arr.entries(), n; ! (n = e.next()).done; ) {
someFn( n.value[ 1 ] );
}
});
suite.add("for( var i = 0, n = arr.length; i < n; ) {", function () {
for( var i = 0, n = arr.length; i < n; ) {
someFn( arr[ i++ ] );
}
});
suite.add("for( var i = 0, n = arr.length; i < n; someFn( arr[ i++ ] ) ) ;", function () {
for( var i = 0, n = arr.length; i < n; someFn( arr[ i++ ] ) ) ;
});
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("Finding out the fastest way to loop through an array");
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