Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex performance
(version: 0)
Comparing performance of:
test vs exec vs match vs indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var html = "<p>A</p><table><tr><td>1</td><td>2</td></tr></table><p>B</p>";
Tests:
test
/<table\b[^>]*>/i.test(html)
exec
/<table\b[^>]*>/i.exec(html)
match
html.match(/<table\b[^>]*>/i)
indexOf
html.toLowerCase().indexOf('<table')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
test
exec
match
indexOf
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
test
6746597.5 Ops/sec
exec
5783589.5 Ops/sec
match
5416325.0 Ops/sec
indexOf
11072875.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, where users can compare the performance of different approaches to test regular expressions in a specific HTML string. **Script Preparation Code** The script preparation code is used to create the input HTML string: ```javascript var html = "<p>A</p><table><tr><td>1</td><td>2</td></tr></table><p>B</p>"; ``` This string contains a table with some text, which will be used as the test data for the regular expression tests. **Html Preparation Code** The HTML preparation code is empty, indicating that no additional HTML strings need to be generated for the benchmark. **Individual Test Cases** There are four individual test cases: 1. **test**: `/<table\\b[^>]*>/i.test(html)` * This test uses the `test()` method of the regular expression object. * It tests whether the input string matches the pattern, with the `i` flag for case-insensitive matching. 2. **exec**: `/<table\\b[^>]*>/i.exec(html)` * This test uses the `exec()` method of the regular expression object. * It attempts to find the first match of the pattern in the input string and returns a result. 3. **match**: `html.match(/<table\\b[^>]*>/i)` * This test uses the `match()` method of the String object. * It searches for all occurrences of the pattern in the input string and returns an array of matches. 4. **indexOf**: `html.toLowerCase().indexOf('<table')` * This test uses the `toLowerCase()` method to convert the input string to lowercase and then uses the `indexOf()` method to find the index of the first occurrence of '<table'. **Libraries and Features** None of these tests use any external libraries or special JavaScript features. **Pros and Cons of Different Approaches** 1. **test()**: This approach is simple and efficient, as it only requires a single pass through the input string. * Pros: Fast and lightweight. * Cons: May not be suitable for large input strings or complex patterns. 2. **exec()**: This approach can return more information about the match, such as the start position and the matched text. * Pros: Can provide more detailed results than `test()`. * Cons: May be slower due to the additional overhead of returning a result object. 3. **match()**: This approach returns an array of all matches in the input string, which can be useful for certain use cases. * Pros: Can return multiple matches and is suitable for searching large input strings. * Cons: May be slower than `test()` due to the additional overhead of creating an array. 4. **indexOf()**: This approach uses a simpler search algorithm that only searches for the first occurrence of the pattern. * Pros: Fast and lightweight, but may not be suitable for finding all occurrences. * Cons: May not be suitable for complex patterns or large input strings. **Other Alternatives** 1. `regex.test()` vs. `test()`: These two methods are similar, but `regex.test()` is a static method that can be called without creating an object. 2. `String.prototype.match()` vs. `match()`: Both methods return an array of matches, but `String.prototype.match()` uses the `g` flag for global matching, which may affect performance. 3. `indexOf()` vs. `search()`: While both methods search for a substring in a string, `indexOf()` is more general and can be used to search for any character sequence, whereas `search()` only searches for a single substring. Overall, the choice of approach depends on the specific requirements of the use case, such as performance, simplicity, or the need to return detailed results.
Related benchmarks:
regex performance with search
regex performance with test and exec
matches vs. closest benchmark
includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?