Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test() vs String.search()
(version: 0)
Comparing performance of:
RegEx reuse vs Regex new
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var REGEX = /abcdef/
Tests:
RegEx reuse
REGEX.test('This is a sample text that should work.')
Regex new
/abcdef/.test('This is a sample text that should work.')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx reuse
Regex new
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx reuse
57202208.0 Ops/sec
Regex new
76144160.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance difference between two approaches to create regular expressions in JavaScript: `test()` and `search()`. This test case is used to evaluate the efficiency of creating regular expressions in different ways. **Options Compared** Two options are compared: 1. **RegEx reuse**: This approach reuses the same regular expression object (`var REGEX = /abcdef/`) for both tests. The `test()` method will check if the string matches the regular expression, while the `search()` method will search for a match in the entire string. 2. **Regex new**: This approach creates a new regular expression object on each test run (`/abcdef/.test('This is a sample text that should work.')`). **Pros and Cons** 1. **RegEx reuse**: * Pros: Reusing the same regular expression object reduces overhead, as the engine can optimize the creation of the regex. * Cons: If the string is modified between tests, it may affect the previous results. 2. **Regex new**: * Pros: Creates a new, isolated regular expression for each test, which might be beneficial if the input strings are different or if the regular expressions need to be updated independently. * Cons: This approach can lead to increased overhead due to the creation of a new regex object. **Other Considerations** The choice between these two approaches depends on the specific use case and requirements. If the same regular expression is used in multiple tests with identical input strings, `RegEx reuse` might be more efficient. However, if different inputs or updated regular expressions are expected, `Regex new` could provide better isolation. **Library Usage** There is no explicitly mentioned library usage in this benchmark. However, some JavaScript engines (like SpiderMonkey) may have internal optimizations that can affect the performance of regular expression creation and execution. **Special JS Feature/Syntax** This benchmark uses a simple regular expression (`abcdef`) without any special features like anchors, word boundaries, or Unicode properties. **Alternatives** Other alternatives to measure the performance difference between `test()` and `search()` methods include: 1. Using different character sets (e.g., alphanumeric vs. non-alphanumeric characters). 2. Creating complex regular expressions with multiple patterns or capturing groups. 3. Testing different string lengths or types (e.g., numeric vs. alphabetical strings). These variations can help you better understand the performance characteristics of your specific use case and ensure that your chosen approach is optimized for your needs. In the provided benchmark result, both tests report a similar execution rate (`ExecutionsPerSecond`) in Firefox 83 on Windows Desktop, suggesting that the choice between `test()` and `search()` methods may not have a significant impact on performance in this specific scenario.
Related benchmarks:
RegEx.test vs String.search
Reuse Regex? RegEx.test vs. String.match vs. String.search
Reuse Global Regex? RegEx.test vs. String.match vs. String.search
RegEx.test vs String.search js
Comments
Confirm delete:
Do you really want to delete benchmark?