Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript startsWith() vs includes()
(version: 0)
Comparing performance of:
startsWith vs includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
startsWith
const str = "utm_source" str.startsWith("utm_")
includes
const str = "utm_source" str.includes("utm_")
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:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
100485752.0 Ops/sec
includes
199727584.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is called "javascript startsWith() vs includes()" and it tests the performance of two JavaScript methods: `startsWith()` and `includes()`. These methods are used to check if a string contains a specific substring. **Options Compared** There are only two options being compared: 1. `startsWith()`: This method checks if the string starts with the specified substring. 2. `includes()`: This method checks if the string contains the specified substring anywhere in it. **Pros and Cons of Each Approach** * **`startsWith()`:** * Pros: * More efficient for certain use cases, as it returns early when the string does not start with the substring. * Can be more readable and easier to maintain in some situations. * Cons: * May not work correctly if the string is empty or null. * Might be slower than `includes()` for large strings because it only checks a single character at a time. * **`includes()`:** * Pros: * Works correctly even when the string is empty or null. * Can be faster than `startsWith()` for large strings, as it uses a more efficient algorithm to search the entire string. * Cons: * Might be slower than `startsWith()` due to its more extensive substring searches. **Library and Syntax** Neither of these methods relies on any specific libraries or advanced JavaScript syntax. They are built-in methods provided by the JavaScript language itself. **Other Considerations** When choosing between `startsWith()` and `includes()`, consider the specific requirements of your use case. If you need to check if a string starts with a substring, `startsWith()` might be more efficient. However, if you need to check for a substring anywhere in the string, regardless of position, `includes()` is likely a better choice. **Other Alternatives** If you don't want to use built-in methods like `startsWith()` and `includes()`, you can create your own implementation using JavaScript loops or regular expressions. However, this approach will generally be slower and less efficient than the built-in methods. In terms of alternatives, there are other JavaScript libraries that provide similar functionality for string manipulation and comparison. For example, Lodash has a `startsWith` method as part of its utility functions, but it's still using the underlying browser implementations for efficiency.
Related benchmarks:
jQuery 3.3.1 selector vs jQuery 1.9.0 vs document.querySelector vs document.getElementById
jQuery 3.3.1 selector vs jQuery 1.8.3 vs document.querySelector vs document.getElementById
getElementById vs id function
React variable vs function for render
Vanilla JS VS JQuery DOM perfomance + getbyid(elId)
Comments
Confirm delete:
Do you really want to delete benchmark?