regexp vs indexOf

JavaScript performance comparison

Revision 16 of this test case created by JD

Preparation code

<div id="foo" class="a foo bar"></div>
      
<script>
Benchmark.prototype.setup = function() {
  var r;
  var element = document.getElementById('foo');
  var value = " ".concat(element.className).concat(" ");
  var reContains = /(?:^| )foo(?: |$)/;
  var dynamicContains = new RegExp('(?:^| )foo(?: |$)');
  
  function dynamicRegExp(value) {
    return RegExp('(?:^| )foo(?: |$)').test(value);
  }
  
  function inlineRegExp(value) {
    return /(?:^| )foo(?: |$)/.test(value);
  }
  
  function dynamicStoredRegExp(value) {
    return dynamicContains.test(value);
  }
  
  function storedRegExp(value) {
    return reContains.test(value);
  }
  
  function stringIndexOf(node) {
    return value.indexOf(' foo ') > -1;
  }

};
</script>

Preparation code output

<div id="foo" class="a foo bar"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
dynamic regexp
r = dynamicRegExp(value);
pending…
inline regexp
r = inlineRegExp(value);
pending…
stored regexp
r = storedRegExp(value);
pending…
string indexOf
r = stringIndexOf(value);
pending…
stored dynamic regexp
r = dynamicStoredRegExp(value);
pending…
Really long string
r = stringIndexOf('really long string with spaces');
pending…
Really long string with dynamic regexp
r = dynamicRegExp('really long string with spaces');
pending…
Really long string with stored dynamic regexp
r = dynamicStoredRegExp('really long string with spaces');
pending…
Really long string with inline regexp
r = inlineRegExp('really long string with spaces');
pending…
Really long string with stored regexp
r = storedRegExp('really long string with spaces');
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.

0 Comments