Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
match vs exec with many matches
(version: 0)
js RegExp.prototype.exec vs String.prototype.match
Comparing performance of:
RegExp#exec with `while` loop vs String#match
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "$".repeat(300); var re = /[$]/g;
Tests:
RegExp#exec with `while` loop
const matches = []; let execResult = []; do { execResult = re.exec(str); if (!execResult) break; matches.push(execResult[0]); } while (true);
String#match
const matches = str.match(re);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegExp#exec with `while` loop
String#match
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegExp#exec with `while` loop
127006.4 Ops/sec
String#match
56056.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **What is being tested?** The benchmark is comparing two approaches for matching multiple occurrences of a pattern in a string: 1. `String.prototype.match()`: This method returns an array of matches or null if no match is found. 2. `RegExp.prototype.exec()` with a `while` loop: This approach uses the regular expression to execute the pattern search, and then loops until no more matches are found. **Options compared** The two approaches being tested are: * `String.prototype.match()`: This method is designed for matching multiple occurrences of a pattern in a string. * `RegExp.prototype.exec()` with a `while` loop: This approach involves calling `exec()` repeatedly to find each match, and then looping until no more matches are found. **Pros and Cons** **`String.prototype.match()`** Pros: * Designed for matching multiple occurrences of a pattern in a string * Efficient and optimized for performance Cons: * Returns an array of matches or null if no match is found (which may not be suitable for all use cases) * Limited control over the search process **`RegExp.prototype.exec()` with a `while` loop** Pros: * Allows for more control over the search process * Can be used to implement custom match logic Cons: * Inefficient and slow compared to `String.prototype.match()` * Requires manual management of the search loop (which can lead to bugs) **Library/Language features** Neither approach relies on a specific library or language feature beyond JavaScript. However, it's worth noting that `RegExp` is a built-in JavaScript object that provides regular expression matching capabilities. **Special JS feature** There is no special JS feature being tested in this benchmark. The focus is solely on comparing two different approaches for matching multiple occurrences of a pattern in a string. **Alternatives** Other alternatives for matching multiple occurrences of a pattern in a string might include: * Using `String.prototype.split()` and `Array.prototype.map()` * Implementing a custom search algorithm using JavaScript loops * Using a third-party library or framework that provides optimized string matching capabilities It's worth noting that the choice of approach will depend on the specific requirements and constraints of the use case. In general, `String.prototype.match()` is the recommended approach for simple string matching tasks, while more complex scenarios may require custom implementation or alternative approaches.
Related benchmarks:
match vs exec
RegEx.exec vs String.match
RegEx.exec vs regex.test
RegEx.exec vs String.match (inline)
Comments
Confirm delete:
Do you really want to delete benchmark?