Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test regexp vs startsWith
(version: 0)
Comparing performance of:
Using regexp vs Using startsWith
Created:
one year ago
by:
Registered User
Go to the latest result
Script Preparation code:
var regexp = /^[iv]-/; function test(value) { return value.startsWith('i-') || value.startsWith('v-'); }
Tests:
Using regexp
regexp.test('i-dynamic-page');
Using startsWith
test('i-dynamic-page');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using regexp
Using startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 YaBrowser/24.6.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Using startsWith
22.6M/s
Using regexp
17.3M/s
View exact numbers
Test name
Executions per second
✓
Using startsWith
22,594,900 Ops/sec
Using regexp
17,259,794 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two different approaches: using a regular expression (`regexp`) and using the `startsWith` method. The script preparation code defines a regular expression (`var regexp = /^[iv]-/;`) that matches strings starting with 'i-' or 'v-'. The test function (`function test(value) { ... }`) uses this regular expression to check if a given string starts with 'i-' or 'v-'. **Options Compared** Two options are compared: 1. **Regular Expression (RegExp)**: The first option uses the `RegExp` object to match strings starting with 'i-' or 'v-'. This approach is typically used for pattern matching and can be slower due to the complexity of the regular expression. 2. **startsWith Method**: The second option uses the `startsWith` method, which is a built-in string method in JavaScript that checks if a string starts with a specified prefix. **Pros and Cons** * **RegExp:** + Pros: - Can match complex patterns (e.g., 'aabbcc'). - Can be used for more advanced text processing tasks. + Cons: - Typically slower due to the overhead of regular expression execution. - Requires explicit pattern definition, which can make it harder to read and maintain. * **startsWith Method:** + Pros: - Faster and more lightweight than RegExp. - Easier to read and maintain due to its simplicity. + Cons: - Limited to simple prefix matching (e.g., 'i-'). **Other Considerations** The benchmark may also consider other factors, such as: * **Browser vs. Engine**: The test results show different execution times for the same string between Yandex Browser 24 and Chrome/124.0.0.0. * **Device Platform and Operating System**: The test results are specific to a desktop Mac OS X 10.15.7. **Library and Special JS Features** There are no libraries mentioned in this benchmark, as it only uses built-in JavaScript methods (`RegExp` and `startsWith`). No special JS features are used, as the code is straightforward and does not employ any advanced or experimental features. **Alternatives** Other alternatives for string matching could include: * **String.prototype.includes()**: Another built-in method that checks if a string contains a specified substring. * **Text Search Algorithms**: More advanced algorithms like Aho-Corasick or Boyer-Moore, which can be more efficient for specific use cases but may have additional complexity and overhead.
Related benchmarks
String.match vs. RegEx.test
Regex type checking comparison
String.match vs. RegEx.test1
Regex vs string compare
Comments
Confirm delete:
Do you really want to delete benchmark?