regexp vs. string

JavaScript performance comparison

Revision 2 of this test case created by 1Pupik1989

Info

Tests matching /hello there/ in a regexp versus in a straight string.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = 'hello there';
    var matched = 0;
    var regexp = /hello there/;
    var charcodes = "hello there".split("").map(function(x) { return x.charCodeAt(0) });
};
</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
regex
if (regexp.exec(str) != null) {
  matched++;
}
pending…
charcodeat
for (var i = 0, len = str.length; i < len; ++i) {
  if (str.charCodeAt(i) != charcodes[i]) {
    break;
  }
  matched++;
}
pending…
test
if (regexp.test(str) == true) {
  matched++;
}
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