Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Demo benchmark fork - pre-initialized regex
(version: 1)
From https://benchmarkjs.com/
Comparing performance of:
RegExp#test vs String#indexOf vs String#match
Created:
one year ago
by:
Guest
Go to the latest result
HTML Preparation code:
<div>This is demo</div>
Script Preparation code:
console.log("js preparation"); var re = /o/;
Tests:
RegExp#test
re.test('Hello World!');
String#indexOf
'Hello World!'.indexOf('o') > -1;
String#match
!!'Hello World!'.match(/o/);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegExp#test
String#indexOf
String#match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
String#indexOf
138.5M/s
RegExp#test
48.6M/s
String#match
27.6M/s
View exact numbers
Test name
Executions per second
✓
String#indexOf
138,540,496 Ops/sec
RegExp#test
48,624,540 Ops/sec
String#match
27,599,248 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests different methods of searching for a character ('o') in the string "Hello World!". The benchmark consists of three test cases evaluating performance variations of string and regular expression operations in JavaScript. ### Test Cases 1. **RegExp#test** - **Benchmark Definition:** `re.test('Hello World!');` - **Description:** This test uses the `test` method of a pre-initialized regular expression (regex) to check if the character 'o' exists within the string. The regex `/o/` is created beforehand in the preparation code. - **Performance Result:** 48,624,540 executions per second. 2. **String#indexOf** - **Benchmark Definition:** `'Hello World!'.indexOf('o') > -1;` - **Description:** This test utilizes the `indexOf` method of the String class, which returns the index of the first occurrence of the specified value ('o'). If the index is greater than -1, it means 'o' is present in the string. - **Performance Result:** 138,540,496 executions per second. 3. **String#match** - **Benchmark Definition:** `!!'Hello World!'.match(/o/);` - **Description:** This test employs the `match` method on the string, which retrieves the matches when matching a string against a regex. The double negation `!!` is used to convert the result into a Boolean value. - **Performance Result:** 27,599,248 executions per second. ### Comparison of Options - **Regex (`RegExp#test`) vs. `indexOf`:** - **Pros of regex:** - More powerful than `indexOf` as it supports complex pattern matching. - Can easily be modified to include additional patterns/extensions. - **Cons of regex:** - Generally slower for simple searches when compared to `indexOf` due to its overhead of parsing the regex pattern and executing the match operation. - **`indexOf` vs. `match`:** - **Pros of `indexOf`:** - Simpler and more efficient for checking the existence of a single character or substring. - **Cons of `indexOf`:** - Limited to finding single characters or substrings unless further logic is added. - **Performance Observations:** - As indicated by the benchmark results, `indexOf` provides the best performance for this simple case, followed by `RegExp#test`, with `String#match` being the slowest. This suggests that for simple existence checks, using `indexOf` is preferable for performance-critical applications. ### Alternatives and Other Considerations - **Alternative Approaches:** - **Using `includes`:** ES6 introduced the `includes` method, which can be used similarly to `indexOf` to check for the existence of a substring. It is more readable ("Hello World!".includes('o')), but its performance characteristics are similar to `indexOf`. - **Utilizing other libraries:** For more complex pattern matching, libraries like `lodash` or a regex library like `XRegExp` can offer more advanced features and performance optimizations. However, these may increase the overhead due to library loading and usage. In summary, the benchmark compares foundational JavaScript string matching techniques, showcasing the performance of regex vs. native string methods in a practical scenario. The results indicate that for this use case, `indexOf` is the optimal choice, while regex and match methods have their place in more complex searches.
Related benchmarks
Demo benchmark
RegEx.test vs. String.includes vs. String.match vs. IndexOf
Comments
Confirm delete:
Do you really want to delete benchmark?