regexp vs indexOf vs substr (v139)

Revision 139 of this benchmark created on


Setup

var string1 = '';
    for (var i = 0; i < 10; ++i) {
     string1 += Math.floor(Math.random() * 10);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
String.prototype.match
var i = 0
if (string1.match(/^012/)) {
 ++i;
}

 
ready
String.prototype.indexOf
var i = 0
if (string1.indexOf("012") === 0) {
 ++i;
}
ready
String.prototype.substr
var i = 0
if (string1.substr(0, 3) === "world") {
  ++i;
}
 
ready
String.prototype.search
// Note that search expects RegExp and converts string to RegExpes.
// This test just checks cost of converting to RegExp.
var i = 0
if (string1.search("012") === 0)++i
 
ready
String.prototype.search with RegExp
var i = 0
if (string1.search(/^012/) === 0)++i
 
ready
String.prototype.search with RegExp without anchor
var i = 0
if (string1.search(/012/) === 0)++i
 
ready

Revisions

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