Last active
September 11, 2015 09:37
-
-
Save maxvipon/2f63b41a4d55aba3bffa to your computer and use it in GitHub Desktop.
bench-number
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
const Benchmark = require('benchmark').Benchmark; | |
const suite = new Benchmark.Suite(); | |
function uid() { | |
return 'xxx'.replace(/x/g, function(c) { | |
return Math.floor(Math.random()*10); | |
}); | |
} | |
const count = 100; | |
var numbers = new Array(count); | |
for (var i = 0; i < count; i++) { | |
numbers[i] = uid(); | |
} | |
suite | |
.add('+', function() { | |
+(numbers[Math.floor(Math.random()*numbers.length)]) | |
}) | |
.add('Number', function() { | |
Number(numbers[Math.floor(Math.random()*numbers.length)]) | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment