Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex-vs-startswith
(version: 0)
Comparing performance of:
regex vs startsWith
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.baseAction = 'MY_ACTION' window.actionRegularExpression = /^MY_ACTION_(\w+)_(?<=_)(.*)$/i; window.baseString = 'MY_ACTION_QUESTION_START_my-first-question';
Tests:
regex
let [, action, id] = actionRegularExpression.exec(baseString)
startsWith
let action; let id; if(baseString.startsWith(baseAction)){ const lastUnderscore = baseString.lastIndexOf('_'); action = baseString.substring(baseAction.length + 1, lastUnderscore) id = baseString.substring(lastUnderscore + 1) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
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; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
15448784.0 Ops/sec
startsWith
52776428.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Definition** The benchmark compares two approaches: `regex` and `startsWith`. The script preparation code defines a regular expression (`actionRegularExpression`) that matches strings starting with "MY_ACTION_". The base string is set to "MY_ACTION_QUESTION_START_my-first-question". **Options Compared** Two options are being compared: 1. **Regex**: Using the defined regular expression to extract the action and id from the base string. 2. **startsWith**: Using the `startsWith` method to check if the base string starts with a specific substring, and then extracting the action and id from the remaining part of the string. **Pros and Cons** * **Regex**: + Pros: Can be more flexible and powerful for complex string matching. + Cons: Can be slower and more resource-intensive due to its compiled nature. * **startsWith**: + Pros: Generally faster and more lightweight than regex, as it uses a simple string comparison. + Cons: Limited to simple substring matching, which may not cover all use cases. **Library Used** There is no specific library mentioned in the benchmark definition. However, JavaScript's built-in `exec` method is used for regex execution, and the `startsWith` method is a standard JavaScript method. **Special JS Feature or Syntax** None are explicitly mentioned, but note that the regex pattern uses a capturing group (`(` and `)`) to extract the action and id from the base string. This is a common technique in regex patterns. **Alternative Approaches** Other alternatives for comparing strings could include: 1. **Substring matching**: Using methods like `indexOf`, `lastIndexOf`, or `substr` to find specific substrings within the original string. 2. **String interpolation**: Using template literals or string concatenation to build the desired output from the input string. Keep in mind that these alternatives might have different performance characteristics and use cases compared to regex and `startsWith`. **Benchmark Considerations** When benchmarking these approaches, it's essential to consider factors like: 1. **String length and complexity**: Longer strings may require more resources or slower execution times. 2. **Regex engine optimization**: Some regex engines offer optimizations for specific patterns, which can impact performance. 3. **Browser and device variations**: Different browsers and devices might exhibit varying performance characteristics due to hardware and software differences. These considerations should be taken into account when preparing and interpreting benchmark results like the ones provided in the question.
Related benchmarks:
RegEx.exec vs regex.test qp
RegEx.test vs. String.includes vs. String.match vs String.match(regex) for starting string
RegEx.test vs. String.includes vs. String.match vs String.startsWith
startsWith or regex test
Test regexp vs startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?