Created
May 3, 2017 03:45
-
-
Save sebdeckers/f110f4c5f65fc70da39587cb5d0fba68 to your computer and use it in GitHub Desktop.
Micro benchmark for and for-of loops in 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
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 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: https://jsperf.co/for-vs-foreach-vs-for-of