Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs startWith
(version: 0)
Comparing performance of:
regex vs startWith
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var foo = ['+++', '---', '===', '@@@', null, undefined, '', 'foo']; var test = []; for (var i = 0; i < 1000; i++) { test = [...test, ...foo]; }
Tests:
regex
test.map((x) => /^\+\-\=\@/.test(x) ? "'" + x : x);
startWith
test.map((x) => { if ( x?.startsWith('+') || x?.startsWith('-') || x?.startsWith('=') || x?.startsWith('@') ) { return "'" + x; } return x; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
startWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Browser/OS:
Chrome 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
10269.8 Ops/sec
startWith
16253.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance difference between two approaches to check if a string starts with a specific character or substring: regular expressions (`regex`) and the `startsWith` method (also known as "string matching" or "substring checking"). In this specific test, both methods are applied to an array of strings `foo`, which contains a mix of strings with different characters, including some null and undefined values. The benchmark is run 1000 times for each string in the array. **Options compared** The two options being compared are: 1. **Regular Expressions (`regex`)**: This approach uses a regular expression to check if a string starts with a specific character or substring. In this case, the regular expression `^\\+\\-\\=\\@/` matches any string that starts with one of these characters. 2. **`startsWith` method**: This is a built-in JavaScript method that checks if a string starts with a specified value. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Regular Expressions (`regex`)**: + Pros: Can be used to check for more complex patterns, can be more efficient than `startsWith` for certain use cases. + Cons: Can be slower than `startsWith` for simple string matching, may have performance implications due to the overhead of compiling regular expressions. * **`startsWith` method**: + Pros: Faster and more lightweight than regular expressions, well-suited for simple string matching. + Cons: May not be as flexible as regular expressions, can be slower for complex patterns. **Library used** In this benchmark, no external library is explicitly mentioned. However, the `test.map()` function is used to apply the testing logic to each element in the array `foo`. This suggests that the benchmark is using built-in JavaScript functionality. **Special JS feature or syntax** There's a subtle point worth mentioning: the use of the optional chaining operator (`?.`) in the `startsWith` method implementation. This operator is only available in ECMAScript 2020 and later versions of JavaScript. In earlier versions, you would need to use explicit null checks instead. **Other alternatives** If you wanted to test other approaches to string matching or substring checking, some possible alternatives could include: * Using a different built-in method, such as `includes()` or `slice()`. * Implementing a custom string matching algorithm using bitwise operations or arithmetic. * Comparing the performance of a third-party library for regular expressions or string matching.
Related benchmarks:
String.match vs. RegEx.test
String.match vs. RegEx.test1
startsWith vs regex hash
Longer regex test vs string includes
RegEx vs Array.includes v2
Comments
Confirm delete:
Do you really want to delete benchmark?