Skip to content

Instantly share code, notes, and snippets.

@sebdeckers
Created May 3, 2017 03:45
Show Gist options
  • Save sebdeckers/f110f4c5f65fc70da39587cb5d0fba68 to your computer and use it in GitHub Desktop.
Save sebdeckers/f110f4c5f65fc70da39587cb5d0fba68 to your computer and use it in GitHub Desktop.
Micro benchmark for and for-of loops in Node.js
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var arr = [1,2,3,4,5,6,7,8,9,0]
suite.add('for', function() {
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i];
item + 1;
}
})
.add('for-of', function() {
for (var item of arr) {
item + 1;
}
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true });
@sebdeckers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment