Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startswith vs includes vs lastindexof
(version: 0)
Comparing performance of:
includes vs startsWith vs lastIndexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const str = "https://firebase.com/this/is/a/long/thing" str.includes("firebase")
startsWith
const str = "https://firebase.com/this/is/a/long/thing" str.startsWith("https://firebase")
lastIndexOf
const str = "https://firebase.com/this/is/a/long/thing" str.lastIndexOf("https://firebase", 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
startsWith
lastIndexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Browser/OS:
Chrome 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
88586480.0 Ops/sec
startsWith
24665292.0 Ops/sec
lastIndexOf
24996284.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the provided benchmark and its results. **Benchmark Overview** The benchmark compares three string comparison methods in JavaScript: `startsWith`, `includes`, and `lastIndexOf`. The goal is to measure which method is the most efficient for searching a specific substring within a longer string. **Test Cases** There are three individual test cases: 1. `str.includes("firebase")`: This test case checks if the string "https://firebase.com/this/is/a/long/thing" contains the substring "firebase". 2. `str.startsWith("https://firebase")`: This test case checks if the string "https://firebase.com/this/is/a/long/thing" starts with the substring "https://firebase". 3. `str.lastIndexOf("https://firebase", 0)`: This test case checks if the string "https://firebase.com/this/is/a/long/thing" contains the substring "https://firebase" starting from index 0. **Library: None** None of the test cases use any external libraries. **Special JS Feature/Syntax: None** There is no special JavaScript feature or syntax used in these benchmark test cases. **Approaches Compared** The three string comparison methods are compared as follows: * `startsWith`: Checks if the string starts with a specified substring. * `includes`: Checks if the string contains a specified substring, regardless of its position. * `lastIndexOf`: Checks if the string contains a specified substring from the beginning, returning the index of the first occurrence. **Pros and Cons** Here's a brief summary of each approach: * `startsWith`: + Pros: Fast and efficient for checking if the string starts with a specific substring. + Cons: Returns only true if the substring is found at the start of the string; may not be suitable for finding occurrences elsewhere in the string. * `includes`: + Pros: Can find any occurrence of the substring, regardless of its position in the string. + Cons: May be slower than `startsWith` since it needs to search through the entire string. * `lastIndexOf`: + Pros: Returns the index of the first occurrence of the substring from the beginning; can be useful for finding occurrences. + Cons: May not be suitable for finding any occurrence, regardless of its position in the string. **Other Considerations** When choosing a string comparison method, consider the following: * If you only need to check if the string starts with a specific substring, `startsWith` might be the best choice. * If you need to find any occurrence of a substring within the string, `includes` might be more suitable. * If you want to find the index of the first occurrence of a substring from the beginning, use `lastIndexOf`. **Alternatives** If you're interested in exploring other alternatives, consider: * Using regular expressions with the `RegExp.prototype.test()` method for more advanced string matching. * Implementing custom string comparison logic using JavaScript's built-in functions and loops. Keep in mind that measuring the performance of these methods can provide valuable insights into the efficiency of your chosen approach.
Related benchmarks:
index vs lastindexof startsWith
String indexOf vs startsWith/endsWith
Js Search - String StartsWith vs Includes
String indexOf() === 0 vs. startsWith()
check application json startswith vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?