Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created March 28, 2016 16:16
Show Gist options
  • Save tmcw/034157189814d0748b58 to your computer and use it in GitHub Desktop.
Save tmcw/034157189814d0748b58 to your computer and use it in GitHub Desktop.
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var strings = [
'hi, there',
'you scoundrel',
'cheers',
'cheers, you',
'cheers, you, there',
'lol',
'donuts'
];
// add tests
suite
.add('split', function() {
strings.map(function(str) {
return str.split(',')[0]
});
})
.add('indexOf', function() {
strings.map(function(str) {
var firstC = str.indexOf(',');
return firstC === -1 ? str : str.substr(0, firstC);
});
})
// 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 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment