regex: capture vs. no capture

JavaScript performance comparison

Revision 5 of this test case created by Joshua Piccari

Info

Testing the performance impact of a capturing group vs. no capturing group inside a regex, also to see if .test() vs. .exec() affects the outcome.

Preparation code

<script>
  var regex_1 = /ab([\s\S]*?)ZZ/g;
  var regex_2 = /ab[\s\S]*?ZZ/g;
  var test_str = "abTesting the performance impact of a capturing group vs. a non-capturing group inside a regexTesting the performance impact of a capturing group vs. a non-capturing group inside a regexZZ"; // 3 matches
  var tmp;
</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
capture group - test
regex_1.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_1.test(test_str);
pending…
capture group - exec
regex_1.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_1.exec(test_str);
pending…
no capture group - test
regex_2.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_2.test(test_str);
pending…
no capture group - exec
regex_2.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_2.exec(test_str);
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