Last active
November 18, 2019 09:52
-
-
Save Aschen/444466793b6780d373749064186c61cc to your computer and use it in GitHub Desktop.
Benchmarking array iteration with Node.js
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
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