Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith or regex test or indexOf
(version: 0)
Comparing performance of:
startsWith vs regex vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.myString = "Hello, world!"; window.prefix = "Hello"; window.regex = /^Hello/;
Tests:
startsWith
if (myString.startsWith(prefix)) { // do something }
regex
if (regex.test(myString)) { // do something }
indexOf
if (myString.indexOf(prefix) === 0) { // do something }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
regex
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 102 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
790289.5 Ops/sec
regex
582831.0 Ops/sec
indexOf
714502.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. The test compares three different approaches to check if a string starts with a certain prefix: 1. `startsWith` method: This is a built-in JavaScript method that checks if a string starts with another string. 2. Regex (`regex`) pattern: This uses a regular expression to match the prefix at the start of the string. 3. `indexOf` method: This method returns the index of the first occurrence of a substring in a string. **What are the pros and cons of each approach?** 1. `startsWith` method: * Pros: Simple, fast, and widely supported across browsers and platforms. * Cons: May be less flexible than regex or `indexOf` if you need to match multiple prefixes or use wildcards. 2. Regex (`regex`) pattern: * Pros: Highly flexible, allowing for complex matching patterns, and can be used for both prefix and suffix matching. * Cons: Slower than `startsWith` due to the overhead of parsing and executing regex patterns. May also require additional setup if you're not familiar with regex syntax. 3. `indexOf` method: * Pros: Can handle multi-character prefixes and allows for starting index offsets, making it useful for finding substrings in specific positions within a string. * Cons: Slower than `startsWith` due to the need to search through the entire string for the prefix. **Other considerations** * In terms of performance, the order of execution might matter. However, since the benchmark is focused on the string comparison itself rather than the overall code execution time, this aspect is less relevant. * For regex, consider that the `^` symbol at the start of the pattern is used to indicate a match from the start of the string. **Library and syntax** None mentioned in the provided JSON, but we can infer that JavaScript's built-in methods (`startsWith`, `indexOf`) are being tested. There are no special JS features or syntax being tested in this benchmark. **Alternatives** Other approaches for checking if a string starts with a certain prefix include: 1. Using a custom function: You could create a simple function that checks each character of the input string against the prefix, returning true as soon as it finds a match. 2. Regular expression alternatives: Other regex patterns might be used to achieve the same result, such as `regex.test(myString) && regex.test(prefix)`. However, these alternatives are less common and more verbose than using built-in JavaScript methods (`startsWith`, `indexOf`).
Related benchmarks:
RegEx.matchAll vs. String.indexOf vs. String.match
startsWith or regex test
Fastest way to do string includes string
Fastest way to do string includes string (ignoring locale)
Comments
Confirm delete:
Do you really want to delete benchmark?