regex: capture vs. non-capture

JavaScript performance comparison

Test case created by Kyle Simpson

Info

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

Preparation code

<script>
  var regex_1 = /ab(cd)/g;
  var regex_2 = /ab(?:cd)/g;
  var test_str = "abcdjnksjdablkjnsabcdaslkd)A-s23-0kwasabkjabcd"; // 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…
non-capture group - test
regex_2.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_2.test(test_str);
pending…
non-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:

1 comment

Dan Beam commented :

Cool, was wondering the outcome of this myself!

Add a comment