regex: capture vs. non-capture

Benchmark created by Kyle Simpson on


Description

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 HTML

<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

Ready to run.

Testing in
TestOps/sec
capture group - test
regex_1.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_1.test(test_str);
ready
capture group - exec
regex_1.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_1.exec(test_str);
ready
non-capture group - test
regex_2.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_2.test(test_str);
ready
non-capture group - exec
regex_2.lastIndex = 0; // reset regex in case browser caches it
tmp = regex_2.exec(test_str);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Kyle Simpson on