Last active
May 16, 2016 08:17
-
-
Save hidekiy/57fdb344bb1c4b921c9f668edf2f91b6 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var Benchmark = require('benchmark'); | |
var _ = require('lodash'); | |
var suite = new Benchmark.Suite; | |
var a = [1, 2, 3]; | |
var stopIteration = new Error('stop iteration'); | |
// add tests | |
suite | |
.add('native-for-break', function() { | |
for (var i = 0, len = a.length; i < len; i++) { | |
break; | |
} | |
}) | |
.add('native-foreach-throw', function() { | |
try { | |
a.forEach(function () { | |
throw stopIteration; | |
}) | |
} catch(e) { | |
if (e !== stopIteration) { | |
throw e; | |
} | |
} | |
}) | |
.add('native-every', function() { | |
a.every(function () { | |
return false; | |
}) | |
}) | |
.add('lodash-foreach', function() { | |
_.forEach(a, function () { | |
return false; | |
}); | |
}) | |
.add('lodash-foreach-throw', function() { | |
try { | |
_.forEach(a, function () { | |
throw stopIteration; | |
}) | |
} catch(e) { | |
if (e !== stopIteration) { | |
throw e; | |
} | |
} | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({async: true}); | |
// Output: | |
// native-for-break x 92,777,547 ops/sec ±0.94% (90 runs sampled) | |
// native-foreach-throw x 1,239,034 ops/sec ±0.66% (82 runs sampled) | |
// native-every x 20,965,765 ops/sec ±0.65% (83 runs sampled) | |
// lodash-foreach x 17,027,859 ops/sec ±0.57% (90 runs sampled) | |
// lodash-foreach-throw x 1,352,385 ops/sec ±0.56% (93 runs sampled) | |
// Fastest is native-for-break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment