Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS > exec vs matchAll vs match
(version: 0)
Comparing performance of:
1111 vs 2222 vs 3333
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
1111
const TAG_REGEX = /(\w+)/g; const dirtyHtml = 'aqui é uma <strong class="elm">questão</strong> de teste <img class="elm" onerror="opa"></img><br class="elm"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/>'; TAG_REGEX.exec(dirtyHtml)
2222
const TAG_REGEX = /(\w+)/g; const dirtyHtml = 'aqui é uma <strong class="elm">questão</strong> de teste <img class="elm" onerror="opa"></img><br class="elm"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/>'; dirtyHtml.matchAll(TAG_REGEX)
3333
const TAG_REGEX = /(\w+)/g; const dirtyHtml = 'aqui é uma <strong class="elm">questão</strong> de teste <img class="elm" onerror="opa"></img><br class="elm"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/><iframe src="http://" class="iframeclass"/>'; dirtyHtml.match(TAG_REGEX)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
1111
2222
3333
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
1111
30525372.0 Ops/sec
2222
23752764.0 Ops/sec
3333
2609012.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its components. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for searching a regular expression in a string: 1. `exec()` 2. `matchAll()` 3. `match()` **Regular Expression** The regular expression used in all benchmarks is: `\w+`, which matches one or more word characters (letters, numbers, and underscores). **Benchmark Preparation Code** The preparation code for each benchmark is identical: ```javascript const TAG_REGEX = /(\\w+)/g; const dirtyHtml = 'aqui é uma <strong class="elm">questão</strong> de teste <img class="elm" onerror="opa"></img><br class="elm"/><iframe src="http://\" class=\"iframeclass\"/><iframe src="http://\" class=\"iframeclass\"/><iframe src="http://\" class=\"iframeclass\"/>'; ``` This code defines the regular expression and a string `dirtyHtml` that contains HTML elements with classes that match the regular expression. **Benchmark Test Cases** There are three test cases, each representing one of the three approaches: 1. **exec()**: This approach uses the `exec()` method to search for the first occurrence of the regular expression in the `dirtyHtml` string. ```javascript TAG_REGEX.exec(dirtyHtml); ``` 2. **matchAll()**: This approach uses the `matchAll()` method to search for all occurrences of the regular expression in the `dirtyHtml` string, returning an iterator with an array of matches. ```javascript TAG_REGEX.matchAll(dirtyHtml) ``` 3. **match()**: This approach uses the `match()` method to search for a single occurrence of the regular expression in the `dirtyHtml` string. The difference between this and `exec()` is that `match()` returns null if no match is found, whereas `exec()` returns an empty array. ```javascript TAG_REGEX.match(dirtyHtml); ``` **Pros and Cons** Here's a brief overview of the pros and cons of each approach: * **exec()**: Pros: + Returns the first match as an object, which can be useful for certain use cases. + Can return null if no match is found. Cons: + May be slower than `matchAll()` or `match()` due to its imperative nature. * **matchAll()**: Pros: + Returns all matches as an iterator with an array of arrays, which can be useful for certain use cases (e.g., processing multiple matches). + Can be faster than `exec()` and `match()` since it uses a more efficient algorithm. Cons: + Requires iterating over the results, which may not be desirable in some cases. * **match()**: Pros: + Returns null if no match is found, which can be useful for certain use cases (e.g., handling missing data). + Can be faster than `exec()` due to its simpler algorithm. **Libraries and Features** There are no external libraries used in these benchmarks. However, it's worth noting that JavaScript's built-in regular expression engine is optimized for performance and provides a good balance between speed and features. No special JavaScript features or syntax are being tested in these benchmarks.
Related benchmarks:
match vs exec
RegEx.exec vs String.match
match vs exec with many matches
split vs exec js
RegEx.exec vs String.match (inline)
Comments
Confirm delete:
Do you really want to delete benchmark?