Created
January 16, 2017 08:17
-
-
Save just-boris/d05c41507167f4dcc09ccc8bed547207 to your computer and use it in GitHub Desktop.
Array for-loop and Array.forEach
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'; | |
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