Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active November 18, 2019 09:52
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 Aschen/444466793b6780d373749064186c61cc to your computer and use it in GitHub Desktop.
Save Aschen/444466793b6780d373749064186c61cc to your computer and use it in GitHub Desktop.
Benchmarking array iteration with Node.js
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite
const array = [];
for (let i = 0; i < 1000; i++) {
array.push(i);
}
suite
.add('for (let i)', () => {
for (let i = 0; i < array.length; ++i) {
const item = array[i];
const foobar = item * 2;
}
})
.add('for (const of)', () => {
for (const item of array) {
const foobar = item * 2;
}
})
.add('foreach', () => {
array.forEach(item => {
const foobar = item * 2;
});
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment