Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regexp.matchAll vs regexp.exec
(version: 0)
Comparing performance of:
regexp.matchAll vs regexp.exec
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
regexp.matchAll
const s = 'Apple Apple Apple Apple Apple'; const r = new RegExp('(^|\\P{L})(Apple)($|\\P{L})', 'giu'); const n = []; const matches = [...s.matchAll(r)]; matches.forEach((match) => { // do something // n.push(match.index); });
regexp.exec
const s = 'Apple Apple Apple Apple Apple'; const r = new RegExp('(^|\\P{L})(Apple)($|\\P{L})', 'giu'); const n = []; let execResults; while ((execResults = r.exec(s)) !== null) { // do something // n.push(execResults.index); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regexp.matchAll
regexp.exec
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regexp.matchAll
1114576.8 Ops/sec
regexp.exec
1802916.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** MeasureThat.net is testing the performance difference between two methods for working with regular expressions in JavaScript: `regexp.matchAll` and `regexp.exec`. The benchmark is designed to measure the number of executions per second (ExecutionsPerSecond) for each method, given a specific string input (`"Apple Apple Apple Apple Apple"`). The goal is to determine which method is faster. **Options compared** Two options are being compared: 1. `regexp.matchAll` 2. `regexp.exec` **Pros and cons of each approach:** * **`regexp.matchAll`**: + Pros: - Returns an iterator that yields arrays with the match result (index, group, etc.) - More memory-efficient since it doesn't create a new array in memory - Can be more flexible for handling multiple matches + Cons: - Requires iterating over the results to extract individual elements - May have performance overhead due to iterator creation and iteration * **`regexp.exec`**: + Pros: - Returns an array with the match result (index, group, etc.) directly - Can be faster since it avoids the need for iteration - Often preferred for its simplicity and readability + Cons: - Creates a new array in memory, which can lead to memory issues for large inputs - May not be as flexible as `matchAll` for handling multiple matches **Library usage** Neither test case uses a specific JavaScript library. The regular expression engine is part of the built-in JavaScript functionality. **Special JS features or syntax** There are no special JavaScript features or syntax used in these benchmark cases. They focus solely on comparing the performance of two regular expression methods. **Other alternatives** If you're interested in exploring alternative approaches, here are a few options: * Use `String.prototype.split()` and `Array.prototype.forEach()` instead of regular expressions to process strings. * Utilize libraries like Lodash or Ramda for more functional programming-style string processing. * Experiment with alternative regex engines like JavaScript's built-in `String.prototype.match()` (though it may not be as efficient as the built-in engine). Keep in mind that these alternatives might introduce additional dependencies, complexity, or performance trade-offs. When working with regular expressions, consider the balance between performance, memory usage, and code readability. MeasureThat.net's benchmark helps demonstrate this trade-off for the specific use case of iterating over match results.
Related benchmarks:
Sanitize HTML String - DomParser vs Regex
replaceAll vs regex replace native
RegEx.exec vs StrRaasdhakshjding.match
Regex tests Dani
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?