Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs includes (same search missing)
(version: 1)
Comparing performance of:
includes vs startsWith
Created:
10 months ago
by:
Guest
Jump to the latest result
Tests:
includes
const str = "https://firebase.com/this/is/a/long/thing" str.includes("https://irebase")
startsWith
const str = "https://firebase.com/this/is/a/long/thing" str.startsWith("https://irebase")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
227036000.0 Ops/sec
startsWith
80501472.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark provided is designed to compare the performance of two JavaScript string methods: `includes` and `startsWith`. Both methods are used to determine if a certain substring exists within a given string, but they operate in different ways and have different use cases. ### Options Compared: 1. **`includes()` Method**: - **Usage**: The `includes()` method checks whether a string contains a specified substring. - **Example in the Benchmark**: ```javascript const str = "https://firebase.com/this/is/a/long/thing"; str.includes("https://irebase"); ``` 2. **`startsWith()` Method**: - **Usage**: The `startsWith()` method tests if a string begins with a specified substring. - **Example in the Benchmark**: ```javascript const str = "https://firebase.com/this/is/a/long/thing"; str.startsWith("https://irebase"); ``` ### Pros and Cons: - **`includes()`**: - **Pros**: - More versatile since it checks for the substring's presence anywhere in the string. - Can be used in scenarios where the exact location of the substring is irrelevant. - **Cons**: - Performance can vary depending on the position of the substring within the string, especially for very long strings. - **`startsWith()`**: - **Pros**: - More straightforward for scenarios where you only care if a string begins with a certain prefix. - Generally more performant for prefix checks because it can stop searching as soon as the start of the string is evaluated. - **Cons**: - Limited to checking prefixes, meaning it's not useful for substring searches beyond the start of the string. ### Benchmark Results Summary: - The results show that the `includes()` method performs significantly better than `startsWith()` in terms of executions per second: - `includes()` executed **227,036,000 times per second**. - `startsWith()` executed **80,501,472 times per second**. ### Other Considerations and Alternatives: - The performance difference observed in the benchmark could vary across different environments, versions of JavaScript engines, and string lengths. It's important for developers to consider the specific requirements of their applications when choosing which method to use. - Other alternatives for substring searching in JavaScript include using regular expressions with the `test()` method, but this approach can be more complex and generally less performant, particularly for simple existence checks. ### Conclusion: In conclusion, when deciding between `includes()` and `startsWith()`, choose based on the specific context of the substring check: if you need a simple prefix check, `startsWith()` is more efficient; if a broader search is needed, `includes()` is the way to go. Understanding the nuances of both methods will help engineers make informed decisions in their JavaScript coding practices.
Related benchmarks:
startswith vs includes
startsWith vs includes (same search)
startswith vs includes - improved
startswith vs includes - same string
startswith vs includes simple
startsWith vs includes('something', 0)
startswith vs includes (exact string)
startswith vs includes test
startswith vs includes for same string
startswith vs includes full search
Comments
Confirm delete:
Do you really want to delete benchmark?