Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs includes and split
(version: 1)
Comparing performance of:
includes vs startsWith
Created:
one year ago
by:
Guest
Go to the latest result
Tests:
includes
const str = "https://firebase.com_/this/is/a/long/thing" const [app, _] = str.split('_') || []; app.includes("https://firebase")
startsWith
const str = "https://firebase.com/this/is/a/long/thing" str.startsWith("https://firebase")
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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
startsWith
45.7M/s
includes
38.8M/s
View exact numbers
Test name
Executions per second
✓
startsWith
45,664,100 Ops/sec
includes
38,755,108 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "startsWith vs includes and split" compares the performance of two string methods in JavaScript: `startsWith` and `includes`. These methods are part of the ES6 (ECMAScript 2015) specification and are frequently used for string manipulation and validation in JavaScript. ### Tested Options 1. **`startsWith` Method**: - **Benchmark Definition**: ```javascript const str = "https://firebase.com/this/is/a/long/thing"; str.startsWith("https://firebase"); ``` - **Test Name**: "startsWith" - **Description**: The `startsWith` method checks if a given string begins with a specified substring. In this case, it checks if `str` starts with `"https://firebase"`. 2. **`includes` Method with `split`**: - **Benchmark Definition**: ```javascript const str = "https://firebase.com_/this/is/a/long/thing"; const [app, _] = str.split('_') || []; app.includes("https://firebase"); ``` - **Test Name**: "includes" - **Description**: In this test, the `split` method is used to split the string at the underscore character (`'_'`). The first part is captured in the variable `app`, which is then checked to see if it includes the substring `"https://firebase"`. ### Performance Results From the benchmark results, we can see the following: - **`startsWith`**: Executions per second are approximately 45,664,100. - **`includes`**: Executions per second are approximately 38,755,108. ### Pros and Cons #### `startsWith` - **Pros**: - Simplicity: Directly checks the beginning of a string without any additional operations. - More efficient: Likely requires less computational overhead since it performs a straightforward check. - **Cons**: - Limited use case: Only checks the starting position, which may not be suitable for scenarios requiring substring search within the entire string. #### `includes` with `split` - **Pros**: - Versatility: Can be useful for validating substrings anywhere in a string after segmenting it. - **Cons**: - Performance: Involves additional operations (first splitting the string and then checking), which can be slower. - Increased complexity: More code, which introduces potential points of failure and makes it harder to read. ### Other Considerations - **Memory Usage**: The `includes` method combined with `split` may use more memory since it creates an array from the split operation before performing the check. - **Use Cases**: `startsWith` is ideal for validating the starting format of a URL or string, while `includes` is useful for checking presence in more complex scenarios where segments of strings are involved. ### Alternatives 1. **Regular Expressions**: You could use regex to test if a string starts with a particular substring. This is flexible but can be less performant than using built-in string methods. ```javascript const regex = /^https:\/\/firebase/; regex.test(str); ``` 2. **`indexOf` Method**: Similar to `includes`, `indexOf` checks if a substring exists in a string. However, it returns the index of the first occurrence rather than a boolean. ```javascript str.indexOf("https://firebase") === 0; // checks if it starts from the beginning. ``` In summary, this benchmark explores the performance of two common string methods, highlighting their respective use cases and trade-offs, which can inform developers on choosing the right approach based on task requirements.
Related benchmarks
startswith vs includes simple
startswith vs includes for same string
startsWith vs includes()
Comments
Confirm delete:
Do you really want to delete benchmark?