Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.replace vs regexp.matchAll vs regexp.exec
(version: 0)
Comparing performance of:
string.replace vs regexp.matchAll vs regexp.exec
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
string.replace
const s = 'Apple Apple Apple Apple Apple'; const r = new RegExp('(^|\\P{L})(Apple)($|\\P{L})', 'giu'); s.replace(r, '*****');
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 (3)
Previous results
Fork
Test case name
Result
string.replace
regexp.matchAll
regexp.exec
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
string.replace
5812066.0 Ops/sec
regexp.matchAll
2778618.0 Ops/sec
regexp.exec
4916916.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The test cases compare three different approaches for processing strings: `string.replace`, `regexp.matchAll`, and `regexp.exec`. The goal is to determine which approach is fastest. **Options Compared** 1. **string.replace**: This method replaces occurrences of a pattern in a string. 2. **regexp.matchAll**: This method returns all matches of a regular expression in a string. 3. **regexp.exec**: This method executes the regex engine on a given string, returning a match array. **Pros and Cons** * `string.replace`: + Pros: Simple to use, efficient for simple replacements. + Cons: Can be slow for complex patterns or large strings. * `regexp.matchAll`: + Pros: Returns all matches, can be useful for finding multiple occurrences. + Cons: May consume more memory and CPU resources than other methods. * `regexp.exec`: + Pros: Efficient for iterating over a string and finding matches one by one. + Cons: Requires manual looping to iterate over matches. **Library** In the test cases, the regular expression library used is built-in JavaScript's `RegExp`. This is because it's part of the standard JavaScript specification. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in these benchmark tests. However, there are some notable features of the regex engine: * The regular expression pattern uses `\P{L}` to match non-lowercase letters. * The `giu` flags at the end of the pattern enable case-insensitive matching and Unicode properties. **Benchmark Preparation Code** The preparation code is empty for all test cases, which means that the benchmarks start with a clean slate. This is good because it allows each approach to be tested in isolation without any overhead from previous runs. **Other Alternatives** If these approaches are not sufficient or desired, other alternatives could include: * Using a dedicated regex engine like PCRE (Perl-Compatible Regular Expressions) or Java's `Pattern` class. * Implementing custom string processing algorithms for better performance. * Using a different programming language with built-in regex support. Keep in mind that each alternative may have its own set of trade-offs and considerations.
Related benchmarks:
replaceAll vs regex DbSgf435
RegEx.exec vs String.match (inline)
split vs exec vs replace
split vs exec vs replace vs slice
RegExp.exec vs String.match vs RegExp.test vs RegExp.match
Comments
Confirm delete:
Do you really want to delete benchmark?