Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Includes vs startsWith speed test
(version: 1)
Comparing performance of:
startsWith vs includes
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
startsWith
const str = "datasetRetentionRelevance" str.startsWith("datasetRetentionRelevance")
includes
const str = "datasetRetentionRelevance" str.includes("datasetRetentionRelevance")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
73114368.0 Ops/sec
includes
261566640.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of the JavaScript string methods `includes()` and `startsWith()` when checking if a string contains a specific substring. ### Benchmark Overview - **Test Cases**: - The first test case evaluates `str.startsWith("datasetRetentionRelevance")`, which checks if the string `str` starts with the specified substring. - The second test case evaluates `str.includes("datasetRetentionRelevance")`, which checks if the specified substring exists anywhere within the string `str`. ### Performance Comparison - **startsWith**: - **Pros**: - Designed specifically to check if a string begins with a defined substring, which can be slightly faster in scenarios where only the beginning of the string needs to be evaluated. - The method's simplicity makes it clear to read and understand what the intent is. - **Cons**: - Limited to checking only the start of the string and cannot be used for other positions. - **includes**: - **Pros**: - More versatile as it can check for the presence of a substring anywhere in the string. - Useful in a variety of scenarios beyond just checking the start. - **Cons**: - Potentially slower than `startsWith` since it needs to check the entire string for the presence of the substring. - May introduce ambiguity if not used carefully, since developers may not be certain whether they are checking specifically for the beginning or just for any occurrence. ### Benchmark Results From the benchmark results, we observe the following: - **includes**: - Executions per second: 169,748,864 - **startsWith**: - Executions per second: 33,478,400 Despite `includes` being more flexible, the `startsWith` method actually has a lower execution rate in this particular test case, suggesting that this specific implementation or environment is optimized for `includes`. This could be due to optimizations in the JavaScript engine or the way these methods are implemented. ### Alternative Approaches Besides `startsWith` and `includes`, there are other alternatives for checking substring presence: - **Regular Expressions**: Using regex (e.g., `/^datasetRetentionRelevance/.test(str)`) can provide powerful capabilities, though they may introduce additional overhead and complexity in performance. - **String.prototype.indexOf()**: This method can also check for substring presence and returns the index of the first occurrence. If the index is `-1`, the substring isn’t present. However, it's less clear than `includes()` for readability, as it requires additional logic to interpret the result. ### Conclusion The benchmark highlights the performance differences between two primary methods in JavaScript for checking string content. Each method has its pros and cons depending on use cases. In scenarios where you need to check for a substring at the start of a string, `startsWith` is appropriate, while `includes` provides broader coverage for substring presence. The choice of which to use should be based on the specific needs of the application, keeping in mind performance characteristics revealed by such benchmarks.
Related benchmarks:
Includes vs ===
startswith vs includes
Includes vs startsWith
javascript startsWith() vs includes()
javascript startsWith() vs includes() for longer string
startswith vs includes simple
startswith vs includes test
indexOf vs startsWith vs includes
startswith vs includes kc
Comments
Confirm delete:
Do you really want to delete benchmark?