startsWith stub indexOf vs regex vs substr + indexOf (short strings) (v4)

Revision 4 of this benchmark created by Pavel on


Setup

var testArray = [],
        stringContent = 'some short content';
    
    for(var i=0; i < 500; i++) {
      testArray.push('R:' + stringContent);
    }
    
    for(i=0; i < 500; i++) {
      testArray.push(stringContent);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
.indexOf
testArray.forEach(function(str) {
  var res = str.indexOf('R:') === 0;
});
ready
//.test
testArray.forEach(function(str) {
  var res = /^R:/.test(str);
});
ready
.substr + .indexOf
testArray.forEach(function(str) {
  var res = str.substr(0, 2).indexOf('R:') === 0;
});
ready

Revisions

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