Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startswith vs includes vs indexOf (leading part of string)
(version: 0)
Comparing performance of:
includes vs startsWith vs indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const str = "https://firebase.com/this/is/a/long/thing" str.includes("https://firebase")
startsWith
const str = "https://firebase.com/this/is/a/long/thing" str.startsWith("https://firebase")
indexOf
const str = "https://firebase.com/this/is/a/long/thing" str.indexOf("https://firebase")>-1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
startsWith
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
1623181824.0 Ops/sec
startsWith
1636730752.0 Ops/sec
indexOf
1620545664.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The test compares three different approaches for checking if a string starts with a certain prefix: `includes`, `startsWith`, and `indexOf`. We will break down each approach, its pros and cons, and discuss other considerations. **Approaches** 1. **`includes`**: This method uses the `includes()` function to check if the entire input string includes the specified substring. 2. **`startsWith`**: This method uses the `startsWith()` function to check if the input string starts with the specified prefix. 3. **`indexOf`**: This method uses the `indexOf()` function to find the index of the first occurrence of the specified substring. **Pros and Cons** 1. **`includes`**: * Pros: Simple, straightforward implementation; works for strings that start with a common prefix but also have other characters. * Cons: Slower performance compared to native methods (`startsWith` and `indexOf`) due to its regex-based approach under the hood. 2. **`startsWith`**: * Pros: Optimized for modern browsers and JavaScript engines, providing faster execution times than `includes`. * Cons: Limited to checking if a string starts with a specific prefix; may not work correctly for strings that have leading whitespace or null/undefined values. 3. **`indexOf`**: * Pros: Fastest approach among the three, as it uses optimized native functions in modern browsers and JavaScript engines. * Cons: Returns `-1` if the substring is not found, which may lead to incorrect results if the intention was to return `false`. **Library and Special JS Features** None of the provided test cases use any libraries or special JavaScript features beyond what is part of the standard language. **Other Considerations** When choosing between these approaches, consider the following factors: * **Performance**: If speed is crucial, `startsWith` is generally the fastest option. However, if you need to support older browsers or more lenient string checks, `includes` might be a better choice. * **String manipulation**: If your strings have leading whitespace or null/undefined values, `startsWith` may not work correctly. In such cases, using a library like `lodash` with its `startsWith` function can provide a safer alternative. * **Browser support**: Ensure that the chosen approach works across various browsers and JavaScript engines. **Alternatives** Other alternatives to these approaches might include: * Using regular expressions (regex) for string matching, which provides more flexibility but also adds overhead. * Implementing custom string matching functions using techniques like Boyer-Moore or Knuth-Morris-Pratt algorithms. * Leveraging libraries like `lodash` or `string-polyfill` to provide polyfilled implementations of these methods in older browsers. These alternatives may offer better performance, flexibility, or compatibility for specific use cases, but often come with additional complexity and overhead.
Related benchmarks:
indexOf vs substr vs startsWith
String indexOf vs startsWith/endsWith
Js Search -String IndexOf vs Includes
Js Search - String StartsWith vs Includes
String indexOf() === 0 vs. startsWith()
Comments
Confirm delete:
Do you really want to delete benchmark?