Skip to content

Instantly share code, notes, and snippets.

@Aschen Aschen/array-benchmark.js
Last active Nov 18, 2019

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