Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs indexOf vs endsWith
(version: 0)
Comparing performance of:
startsWith vs indexOf(start) vs endsWith vs indexOf(end)
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var start = 'hello!'; var end = 'string!'; var string = start + ' im a ' + end;
Tests:
startsWith
string.startsWith(start);
indexOf(start)
string.indexOf(start) == 0;
endsWith
string.endsWith(end);
indexOf(end)
string.indexOf(end) == (string.length - end.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
startsWith
indexOf(start)
endsWith
indexOf(end)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
46263836.0 Ops/sec
indexOf(start)
98543680.0 Ops/sec
endsWith
37383256.0 Ops/sec
indexOf(end)
103139416.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The MeasureThat.net website provides a platform for users to create and run JavaScript microbenchmarks. In this case, we have a benchmark that compares the performance of three different methods to check if a string starts with a certain substring: `startsWith`, `indexOf` (with and without equality), and `endsWith`. **Benchmark Definition** The benchmark definition JSON provides information about the benchmark itself: ```json { "Name": "startsWith vs indexOf vs endsWith", "Description": null, "Script Preparation Code": "var start = 'hello!';\r\nvar end = 'string!';\r\nvar string = start + ' im a ' + end; ", "Html Preparation Code": null } ``` The script preparation code defines three variables: `start`, `end`, and `string`. The `string` variable is constructed by concatenating the `start` and `end` strings with an intermediate string. **Test Cases** The individual test cases are defined in a JSON array: ```json [ { "Benchmark Definition": "string.startsWith(start);", "Test Name": "startsWith" }, { "Benchmark Definition": "string.indexOf(start) == 0;", "Test Name": "indexOf(start)" }, { "Benchmark Definition": "string.endsWith(end);", "Test Name": "endsWith" }, { "Benchmark Definition": "string.indexOf(end) == (string.length - end.length);", "Test Name": "indexOf(end)" } ] ``` Each test case defines a different benchmark definition, which is executed to measure its performance. **Library and Syntax** In the script preparation code, the `concat` method is used to concatenate strings: ```javascript var string = start + ' im a ' + end; ``` The `concat` method returns a new string created by concatenating the individual arguments. This is not a native JavaScript function, but rather a part of the ECMAScript standard. **Pros and Cons** Here's a brief overview of each approach: 1. **startsWith**: This method checks if the string starts with the specified substring. * Pros: Simple and efficient, as it only needs to check the first few characters of the string. * Cons: May be slower for very long strings, as it has to iterate over the entire string. 2. **indexOf (with equality)**: This method checks if the index of the specified substring is equal to 0. * Pros: More efficient than `startsWith` for large strings, as it only needs to check a small portion of the string. * Cons: Requires an additional comparison with 0, which can be slower. 3. **endsWith**: This method checks if the string ends with the specified substring. * Pros: Similar to `startsWith`, but more efficient for very long strings, as it only needs to check the last few characters. * Cons: May be slower for short strings, as it has to iterate over the entire string. **Alternatives** Other alternatives could include: 1. **RegExp**: Using a regular expression with the `^` anchor can achieve similar results to `startsWith`. However, this approach may be slower and less efficient than the built-in methods. 2. **Substring extraction**: Extracting the substring using slicing or concatenation can also be used to implement these methods. In summary, while each method has its pros and cons, `indexOf` (with equality) is generally the most efficient approach for checking if a string starts with or ends with a certain substring.
Related benchmarks:
index vs lastindexof startsWith
String indexOf vs startsWith/endsWith
Js Search - String StartsWith vs Includes
endswith vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?