Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs regex 2
(version: 0)
Comparing performance of:
startsWith vs equals and startsWith vs regex
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { __protot__: '__protot__', _prefixA: '_prefixA', _prefixB: '_prefixB', _prefixC: '_prefixC', _prefixD: '_prefixD', prefixA: '_prefixA', prefixB: '_prefixB', prefixC: '_prefixC', prefixD: '_prefixD', }
Tests:
startsWith
var count = 0 for (const key of Object.keys(obj)) { if (key.startsWith('__') || key.startsWith('_prefix')) count++ }
equals and startsWith
var count = 0 for (const key of Object.keys(obj)) { if (key === '__protot__' || key.startsWith('_prefix')) count++ }
regex
var count = 0 for (const key of Object.keys(obj)) { if (/^_(_|prefix)/.test(key)) count++ }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
equals and startsWith
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
4963087.5 Ops/sec
equals and startsWith
5404366.0 Ops/sec
regex
4355446.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared, and other considerations. **Benchmark Overview** The benchmark compares three approaches to check if a key in an object starts with a specific prefix: `startsWith`, `equals and startsWith`, and regular expression (`regex`). **Options Compared** 1. **startsWith**: Checks if the key starts with a specified prefix using the `startsWith()` method. 2. **equals and startsWith**: First checks if the key is equal to a specified value, then checks if it starts with another specified prefix using the `startsWith()` method. This approach is not optimized for performance as it performs two separate checks. 3. **regex**: Uses a regular expression (`/^_(_|prefix)/`) to match the key against the specified prefix. **Pros and Cons** 1. **startsWith**: * Pros: Fast, concise, and widely supported. * Cons: May not work correctly with non-English characters or special keys (e.g., `__protot__`). 2. **equals and startsWith**: * Pros: None notable. * Cons: Inefficient, as it performs two separate checks. Not recommended for performance-critical applications. 3. **regex**: * Pros: Flexible and powerful, can match complex patterns. * Cons: Slower than `startsWith` due to the overhead of regular expression parsing and execution. **Library/Functionality Used** 1. The benchmark uses JavaScript's built-in `Object.keys()` function to iterate over an object's keys. 2. The `startsWith()` method is used directly in two test cases (`equals and startsWith` and `regex`). **Special JS Feature/Syntax** None mentioned, but note that some older browsers may not support the `Object.keys()` function or `startsWith()` method. **Other Alternatives** 1. For a more efficient alternative to `startsWith`, you could use a library like Lodash's `_.startsWith()` function. 2. If you need even greater flexibility with regular expressions, consider using a dedicated regex engine like RegExp.js. 3. In some cases, you might prefer to use a different approach, such as checking if the key is an exact match or using a prefix-based data structure (e.g., a trie). Keep in mind that these alternatives may have their own trade-offs in terms of performance, complexity, and compatibility with older browsers or environments.
Related benchmarks:
Regex vs split/includes
string startswith vs regexp test
startsWith vs regex hash
RegEx.test vs. String.includes vs. String.match vs String.startsWith
endsWith vs Regex-literal
Comments
Confirm delete:
Do you really want to delete benchmark?