Skip to content

Instantly share code, notes, and snippets.

@just-boris
Created January 16, 2017 08:17
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 just-boris/d05c41507167f4dcc09ccc8bed547207 to your computer and use it in GitHub Desktop.
Save just-boris/d05c41507167f4dcc09ccc8bed547207 to your computer and use it in GitHub Desktop.
Array for-loop and Array.forEach
'use strict';
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var forLoop = {
setup: () => {
var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
this.arr = arr;
},
fn: () => {
function someFn(i) {
return i * 3 * 8;
}
var arr = this.arr;
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
}
};
var forEach = {
setup: () => {
var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
this.arr = arr;
},
fn: () => {
this.arr.forEach(i => i * 3 * 8);
}
};
// add tests
suite
.add('for loop', forLoop)
.add('for each', forEach)
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment