Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regexp capturing group named vs indexed
(version: 0)
Comparing performance of:
named group vs indexed group
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var includeRegexpNamed = /=include\((?<file>.+?)(?:\)|,\s*{(?<data>.+?)}\))/g; var includeRegexpIndexed = /=include\((.+?)(?:\)|,\s*{(.+?)}\))/g; var str = "qqqqqqq qqqqqqqq qqqqqqqqqqqqqq let res=include('./file.eta', { name: 'eta' }) qqqqqqq qqqqqqqq qqqqqqqqqqqqqq qqqqqqq";
Tests:
named group
var result = str.replaceAll(includeRegexpNamed, `=require($<file>)({...it, ...{$<data>}})`);
indexed group
var result = str.replaceAll(includeRegexpIndexed, `=require($1)({...it, ...{$2}})`);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
named group
indexed group
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
named group
4607536.0 Ops/sec
indexed group
5255761.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for replacing regular expressions in a string: one using named capture groups (`includeRegexpNamed`) and another using indexed capture groups (`includeRegexpIndexed`). **Regular Expressions (Regex)** In this context, a regex is used to match the string `str`, which contains placeholders that will be replaced with actual values. The regex pattern is: `=require($<file>)({...it, ...{$<data>}})` The `<file>` and `<data>` parts are placeholders for values that need to be included in the replacement. **Named Capture Groups (`includeRegexpNamed`)** In this approach, named capture groups are used. A named group is a regex feature that allows you to create a named reference to a capture group. In this case, `includeRegexpNamed` uses: `=include\\((?<file>.+?)(?:\\)|,\\s*{(?<data>.+?)}\\))/g` Here, `<file>` and `<data>` are named capture groups (`(?<file>.+?)` and `(?<data>.+?)`, respectively). The `g` flag at the end makes the regex global. When using named capture groups, the replacement string becomes: `=require($<file>)({...it, ...{$<data>}})` **Indexed Capture Groups (`includeRegexpIndexed`)** In this approach, indexed capture groups are used. An indexed group is a regex feature that allows you to create an index-based reference to a capture group. In this case, `includeRegexpIndexed` uses: `=include\\((.+?)(?:\\)|,\\s*{(.+?)}\\))/g` Here, `$<file>` and `$<data>` are indexed capture groups (`(.+?)` and `(.+?`, respectively). The `1` and `2` in the replacement string refer to these indices. When using indexed capture groups, the replacement string becomes: `=require($1)({...it, ...{$2}})`` **Options Compared** The benchmark compares two options: 1. **Named Capture Groups (`includeRegexpNamed`)**: uses named reference for capturing groups. * Pros: + Easier to read and maintain, as the capture group names are explicitly defined. + Can be more readable and self-documenting. * Cons: + May have performance overhead due to the added complexity of named captures. 2. **Indexed Capture Groups (`includeRegexpIndexed`)**: uses index-based reference for capturing groups. * Pros: + Typically faster, as there's less overhead in parsing the indices. + Can be more efficient in terms of execution speed. * Cons: + Requires careful management of indices to avoid errors or unexpected behavior. **Library Used** In this benchmark, `include` is a built-in function for including files or data into the replacement string. Its purpose is to allow you to include values from external sources (files) into your regex pattern. **Special JS Feature/Syntax** This benchmark uses JavaScript's named and indexed capture groups features, which are specific to ECMAScript 2015+ syntax. **Other Alternatives** For replacing regex patterns in strings, there are other approaches and libraries available. Some alternatives include: 1. **String.prototype.replaceAll**: a built-in method for replacing substrings in a string. 2. **regex-replace**: a library for regular expression replacement in Node.js. 3. **replace**: a built-in function or a library like `lodash.replace` for replacing regex patterns. These alternatives may have different performance characteristics, syntax, and usage compared to the named and indexed capture groups approach used in this benchmark.
Related benchmarks:
includes vs regex
RegEx.matchAll vs includes no match
RegEx.matchAll vs includes with match
test regex vs regex vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?