regexp vs indexOf vs substr

JavaScript performance comparison

Revision 139 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
   
    var string1 = '';
    for (var i = 0; i < 10; ++i) {
     string1 += Math.floor(Math.random() * 10);
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

 
pending…
String.prototype.indexOf
var i = 0
if (string1.indexOf("012") === 0) {
 ++i;
}
pending…
String.prototype.substr
var i = 0
if (string1.substr(0, 3) === "world") {
  ++i;
}
 
pending…
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
 
pending…
String.prototype.search with RegExp
var i = 0
if (string1.search(/^012/) === 0)++i
 
pending…
String.prototype.search with RegExp without anchor
var i = 0
if (string1.search(/012/) === 0)++i
 
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment