Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp vs startsWith
(version: 0)
Comparing performance of:
regexp vs startsWith vs includes vs indexOf
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
regexp
const notAbsolute = (link) => !/^https?:\/\//.test(link); notAbsolute('https://www.measurethat.net/Benchmarks/Add') notAbsolute('http://www.measurethat.net/Benchmarks/Add') notAbsolute('/Benchmarks/Add')
startsWith
const notAbsolute = (link) => !link.startsWith('https://') && !link.startsWith('http://'); notAbsolute('https://www.measurethat.net/Benchmarks/Add') notAbsolute('http://www.measurethat.net/Benchmarks/Add') notAbsolute('/Benchmarks/Add')
includes
const notAbsolute = (link) => !link.includes('://'); notAbsolute('https://www.measurethat.net/Benchmarks/Add') notAbsolute('http://www.measurethat.net/Benchmarks/Add') notAbsolute('/Benchmarks/Add')
indexOf
const notAbsolute = (link) => link.indexOf('://') === -1; notAbsolute('https://www.measurethat.net/Benchmarks/Add') notAbsolute('http://www.measurethat.net/Benchmarks/Add') notAbsolute('/Benchmarks/Add')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regexp
startsWith
includes
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regexp
15099987.0 Ops/sec
startsWith
479829088.0 Ops/sec
includes
793022656.0 Ops/sec
indexOf
796914304.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of three different string comparison methods in JavaScript: `RegExp`, `startsWith`, `includes`, and `indexOf`. These methods are designed to check if a given string does not start with a specific prefix. **Comparison Methods** Here's what each method does: 1. **RegExp**: This method uses a regular expression pattern to match the string against the specified prefix. In this case, the pattern `/^https?:\/\//` checks for the presence of either "http://" or "https://" at the start of the string. 2. **startsWith**: This method simply checks if the string starts with the specified prefix using the `startsWith()` method. 3. **includes**: This method checks if the string contains the specified prefix, regardless of its position in the string. However, it's worth noting that this method is not exactly equivalent to checking for a prefix, as it will return true even if the prefix is part of another substring within the original string. 4. **indexOf**: This method finds the index of the first occurrence of the specified prefix in the string and returns -1 if the prefix is not found. **Pros and Cons** Here's a brief summary of each method's pros and cons: * **RegExp**: + Pros: Highly flexible and can match complex patterns. + Cons: Can be slower than other methods due to the overhead of creating an object from the pattern, and may have performance issues with very large strings or complex patterns. * **startsWith**: + Pros: Fast and straightforward to implement. + Cons: Limited flexibility compared to RegExp, as it only checks for a prefix at the start of the string. * **includes**: + Pros: Simple to implement and can handle partial matches. + Cons: May return unexpected results due to its behavior on strings with partial matches. * **indexOf**: + Pros: Fast and efficient, especially for large strings. + Cons: Returns -1 if the prefix is not found, which may be considered an error. **Library Used** In this benchmark, none of the methods use a dedicated library. However, the `RegExp` method does use the `/` character to denote the start of a pattern, which is specific to JavaScript and requires understanding of regular expressions. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark beyond what's required for string comparison.
Related benchmarks:
Case Insensitive RegEx.test vs. String.includes vs String.startsWith
string startswith vs regexp test
startsWith vs regex hash
RegEx.test vs. String.includes vs. String.match vs String.startsWith
includes with regex vs startWith
Comments
Confirm delete:
Do you really want to delete benchmark?