starts_with (v13)

Revision 13 of this benchmark created by Alexis Martial on


Description

fastest starts-with string match

Setup

var arr = [],
        i, str = "abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba abdef abc";
    
    for (i = 0; i < 10; i++) {
      arr.push(str.charAt(i % 26) + str);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
arr.filter(function(s) {
  return s.indexOf('abcde') === 0;
});
ready
regexp
arr.filter(function(s) {
  return !!s.match(/^abcde/);
});
ready
substring
arr.filter(function(s) {
  return s.substring(0, 'abcde'.length) === 'abcde';
});
ready
while, charAt()
arr.filter(function(s) {
  var c = 'abcde'.length;

  if (s.length<c)
    return false;
  var i=0;
  while (i!==c) {
    if (s.charAt(i)!=='abcd'.charAt(i))
      return false;
    i++;
  }
  return true;
});
ready

Revisions

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