RegExp.test vs. String.search

JavaScript performance comparison

Revision 24 of this test case created

Info

Check performance of simple regex use case of seeing whether a candidate string matches a regex.

Preparation code

<script>
  var strings = [];
  for (var i = 0; i < 500; ++i) {
    strings[i] = "The end of the beginning? Or the beginning of the end?";
  }

  var regex1 = /beginning/,
      regex2 = /(end of).*(end)/,
      regex3 = /beginnizg/;

  var simpleWrapper = function (regex, string) {
     string.search(regex) > -1;
  };
</script>

Test runner

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

Java applet disabled.

Testing in Chrome 46.0.2490.86
Test Ops/sec
String.search
var list = [];
for (var i = 0; i < strings.length; ++i) {
  var b = strings[i].search(regex1) > -1,
      b2 = strings[i].search(regex2) > -1,
      b3 = strings[i].search(regex3) > -1;
  list.push(b || b2 || b3);
}
pending…
String.match
var list = [];
for (var i = 0; i < strings.length; ++i) {
  var b = strings[i].match(regex1) !== null,
      b2 = strings[i].match(regex2) !== null,
      b3 = strings[i].match(regex3) !== null;
  list.push(b || b2 || b3);
}
pending…
RegExp.test
var list = [];
for (var i = 0; i < strings.length; ++i) {
  var b = regex1.test(strings[i]),
      b2 = regex2.test(strings[i]),
      b3 = regex3.test(strings[i]);
  list.push(b || b2 || b3);
}
pending…
RegExp.exec
var list = [];
for (var i = 0; i < strings.length; ++i) {
  var b = regex1.exec(strings[i]) !== null,
      b2 = regex2.exec(strings[i]) !== null,
      b3 = regex3.exec(strings[i]) !== null;
  list.push(b || b2 || b3);
}
pending…
String.search wrapped
var list = [];
for (var i = 0; i < strings.length; ++i) {
  var b = simpleWrapper(regex1, strings[i]),
      b2 = simpleWrapper(regex2, strings[i]),
      b3 = simpleWrapper(regex3, strings[i]);
  list.push(b || b2 || b3);
}
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

Comment form temporarily disabled.

Add a comment