Created
July 21, 2017 10:00
-
-
Save fujiwara/d495ce8570d888e4254dae548a0e95e1 to your computer and use it in GitHub Desktop.
node splitN benchmark
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; | |
suite.add('indexOf', function() { | |
var s = "time:2017-01-01T11:22:33Z" | |
var i = s.indexOf(":") | |
return [ s.slice(0,i), s.slice(i+1) ]; | |
}) | |
.add("replace\\0", function() { | |
var s = "time:2017-01-01T11:22:33Z" | |
return s.replace(":", "\0").split("\0"); | |
}) | |
.add("regexp", function() { | |
var s = "time:2017-01-01T11:22:33Z" | |
var m = s.match(/^(.*?):(.*)$/); | |
return [ m[1], m[2] ]; | |
}) | |
.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 }); |
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
$ node -v | |
v7.0.0 | |
$ node splitN.js | |
indexOf x 7,990,197 ops/sec ±2.28% (82 runs sampled) | |
replace\0 x 1,737,003 ops/sec ±1.56% (85 runs sampled) | |
regexp x 6,124,354 ops/sec ±1.91% (86 runs sampled) | |
Fastest is indexOf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment