starts_with (v15)

Revision 15 of this benchmark created by odignal on


Description

fastest starts-with string match

Setup

var arr = [],
        i, str = "abcdefghijklmnopqrstuvwxyz";
    
    for (i = 0; i < 1000000; i++) {
      arr.push(str.charAt(i % 26) + str);
    }
    
    var name="abcde";

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
/*arr.filter(function(s) {
  return s.indexOf('a') === 0;
});*/
return str.indexOf(name) == 0;
ready
regexp
/*arr.filter(function(s) {
  return !!s.match(/^a/);
});*/
return !!str.match(/^abcde/);
ready
substring
/*arr.filter(function(s) {
  return s.substring(0, 1) === 'a';
});*/
return str.substring(0, name.length) == name;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.