Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string start with method startsWith Vs. indexOf Vs. String Array
(version: 1)
Comparing performance of:
startsWith vs indexOf vs String Array
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.testStr = 'mt-asjhbfdjasasdkbfas'; window.counter = 0; window.testTimes = 1e5;
Tests:
startsWith
window.counter = 0; for (let index = 0; index < testTimes; index++) { if (testStr.startsWith('mt-')) { counter++; } }
indexOf
window.counter = 0; for (let index = 0; index < testTimes; index++) { if (testStr.indexOf('mt-') === 0) { counter++; } }
String Array
window.counter = 0; for (let index = 0; index < testTimes; index++) { if (testStr[0] === 'm' && testStr[1] === 't' && testStr[2] === '-') { counter++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
indexOf
String Array
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 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
171.6 Ops/sec
indexOf
157.7 Ops/sec
String Array
166.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark evaluates the performance of three different methods for checking if a string starts with a specific substring ("mt-"). The methods compared are: 1. **Using `startsWith()` method**: - **Code**: `if (testStr.startsWith('mt-'))` - **Pros**: - Readable and semantically clear for the task of checking prefixes. - Built-in method in JavaScript, optimized for this specific operation. - **Cons**: - May have performance overhead when compared with basic string indexing, depending on implementation details across different engines. 2. **Using `indexOf()` method**: - **Code**: `if (testStr.indexOf('mt-') === 0)` - **Pros**: - Also provides clear functionality for finding substrings. - Works in older browsers that might not support `startsWith()`. - **Cons**: - Less efficient for prefix checks since it scans through the string to find the index (in this case, `0`). - Not as semantically clear as `startsWith()` for checking if a string starts with a specific substring. 3. **Using direct character comparison**: - **Code**: `if (testStr[0] === 'm' && testStr[1] === 't' && testStr[2] === '-')` - **Pros**: - Can be the fastest method since it directly accesses and compares characters without any method call overhead. - Minimal overhead, as it does not involve function calls or searching through a substring. - **Cons**: - Less readable and maintainable as it relies on hard-coded position checks. - Not scalable for longer prefixes or varying lengths. ### Performance Results In the benchmark results, the performance in terms of executions per second is as follows: 1. **`startsWith()`**: 171.58 executions per second 2. **Direct character comparison**: 165.96 executions per second 3. **`indexOf()`**: 157.69 executions per second ### Considerations - **Readability vs. Performance**: The trade-off between using built-in methods for code clarity and directly accessing string characters for performance is a critical consideration. For large applications or performance-critical code, the direct method may be preferable, but for general use, `startsWith()` provides better readability. - **Browser Compatibility**: If developing for environments requiring compatibility with older browsers, `indexOf()` offers broader support, whereas `startsWith()` requires modern JavaScript engines. - **Maintainability**: The methods that leverage built-in string functions are usually preferred in collaborative environments where code must be readable for future developers. ### Alternatives Other alternatives to check if a string starts with a specific substring could include: - **Regular Expressions**: Using regex (e.g., `/^mt-/`) can also check for prefixes, providing a flexible option (although generally slower than the above methods). - **Third-party libraries**: Libraries like Lodash provide utility functions for string operations, but they introduce weight to your project. In summary, each method of checking a prefix has its scenarios where it shines; performance considerations need to be weighed against usability and maintainability in development environments.
Related benchmarks:
else vs continue
else vs continue vs short if
concat VS + VS +=
let vs const vs var 2
String Comparison vs Int Comparison
while vs do while
for vs for of2
for vs for of3
loop vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?