compute regex vs substr

JavaScript performance comparison

Revision 7 of this test case created

Info

http://stackoverflow.com/questions/11818896/how-do-i-make-an-anchored-regex-match-in-the-middle-of-a-string-in-javascript (edit 1) bugfix: don't substr in case 1. The RegExp moves the starting position.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var target = "foobar barse";
   
    var str_match = "bar";
    var pre_regex = /^bar/;
};
</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
compute
for (var i = 0; i < 10; i++) 
    new RegExp("^[\\S\\s]{" + i + "}" + str_match).test(target);
pending…
substr+pre
for (var i = 0; i < 10; i++) 
    pre_regex.test(target.substr(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