regex: capture vs. non-capture
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
capture group - test |
|
pending… |
capture group - exec |
|
pending… |
non-capture group - test |
|
pending… |
non-capture group - exec |
|
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:
- Revision 1: published by Kyle Simpson
- Revision 2: published by cHao
1 comment
Cool, was wondering the outcome of this myself!