Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created July 21, 2017 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/d495ce8570d888e4254dae548a0e95e1 to your computer and use it in GitHub Desktop.
Save fujiwara/d495ce8570d888e4254dae548a0e95e1 to your computer and use it in GitHub Desktop.
node splitN benchmark
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 });
$ 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