Skip to content

Instantly share code, notes, and snippets.

@jclem
Last active April 12, 2016 19:31
Show Gist options
  • Save jclem/e85a026c6d9dde57ba273f3a12d95123 to your computer and use it in GitHub Desktop.
Save jclem/e85a026c6d9dde57ba273f3a12d95123 to your computer and use it in GitHub Desktop.
Benchmark
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
suite.add('String#match', function() {
var match = 'foo,bar,baz'.match(/^(foo),(bar),(baz)/);
var foo = match[1];
var bar = match[2];
var baz = match[3];
}).add('String#split', function() {
var split = 'foo,bar,baz'.split(',');
var foo = split[0];
var bar = split[1];
var baz = split[2];
}).add('Slice, String#split', function() {
var string = 'foobar,baz';
var foo = string.slice(0,3);
var split = string.slice(3).split(',');
var bar = split[0];
var baz = split[1];
}).on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).run();
{
"dependencies": {
"benchmark": "^2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment