Skip to content

Instantly share code, notes, and snippets.

@just-boris just-boris/index.js
Created Jan 16, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.