Skip to content

Instantly share code, notes, and snippets.

@eldargab
Last active December 15, 2015 19:19
Show Gist options
  • Save eldargab/5310866 to your computer and use it in GitHub Desktop.
Save eldargab/5310866 to your computer and use it in GitHub Desktop.
string startsWith() benchmark
var Suite = require('benchmark').Suite
function startsWith(prefix, str) {
for (var i = 0; i < prefix.length; i++) {
if (prefix[i] != str[i]) return false
}
return true
}
var suite = new Suite
suite.add('startsWith()', function() {
startsWith('hello', 'hello world')
})
suite.add('indexOf() == 0', function(deferred) {
'hello world'.indexOf('hello') == 0
})
suite.add('regex', function(deferred) {
/^hello/.test('hello world')
})
suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run({async: true})
@eldargab
Copy link
Author

eldargab commented Apr 4, 2013

results

startsWith() x 22,076,291 ops/sec ±0.45% (94 runs sampled)
indexOf() == 0 x 18,575,700 ops/sec ±1.57% (89 runs sampled)
regex x 21,090,471 ops/sec ±0.63% (93 runs sampled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment