indexOf vs regxp & substring vs replace

JavaScript performance comparison

Test case created

Preparation code

<div>
</div>
<script>
Benchmark.prototype.setup = function() {
    var loc = location.href;
    var t;
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
simple indexOf
t = loc.indexOf('com');
pending…
IndexOf, getting substring
t = loc.substring(loc.indexOf('com') + 5);
pending…
IndexOf, getting substring2
t = loc.substring(loc.indexOf('js') + 1, loc.indexOf('rf'));
pending…
IndexOf, excluding substringIndexOf, excluding substring
t = loc.substring(0, loc.indexOf('js')) + loc.substring(loc.indexOf('rf'));
pending…
simple regxp
t = loc.search(/jspref/g);
pending…
regxp replace
t = loc.replace(/jspre/g, '');
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