Last active
December 15, 2015 19:19
-
-
Save eldargab/5310866 to your computer and use it in GitHub Desktop.
string startsWith() benchmark
This file contains hidden or 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 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}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
results