Last active
June 28, 2020 03:14
-
-
Save joelibaceta/058e0ef27b868cec43a8f2fc8e830f92 to your computer and use it in GitHub Desktop.
For, ForEach ForOf Benchmark
This file contains hidden or 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
"use strict"; | |
var Benchmark = require('benchmark'); // Using https://benchmarkjs.com | |
const dogs = Array(1000000).fill("Doggy"); // One million of items | |
var suite = new Benchmark.Suite; | |
suite.add('for#test', function() { | |
for(let i = 0; i < dogs.lenght; i++){ | |
return dog[i] | |
} | |
}) | |
.add('for_caching#test', function() { | |
const size = dogs.lenght; | |
for(let i = 0; i < size; i++){ | |
return dog[i] | |
} | |
}) | |
.add('forEach#test', function() { | |
dogs.forEach(function(dog, i){ | |
return dog; | |
}); | |
}) | |
.add('forof#test', function() { | |
for(const dog of dogs.entries()){ | |
return dog; | |
} | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
suite.run({ 'async': true }); |
This file contains hidden or 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
Fastest is for_caching#test |
This file contains hidden or 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
for#test x 55,659,660 ops/sec ±2.94% (78 runs sampled) |
This file contains hidden or 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
for_caching#test x 56,174,776 ops/sec ±3.19% (79 runs sampled) |
This file contains hidden or 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
forEach#test x 7,783,568 ops/sec ±4.54% (76 runs sampled) |
This file contains hidden or 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
forof#test x 5,088,843 ops/sec ±4.21% (79 runs sampled) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment