Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String indexOf vs startsWith/endsWith
(version: 0)
Comparing performance of:
indexOf vs startsWith vs endsWith
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "penpineappleapplepen"
Tests:
indexOf
str.indexOf('pen') !== 1
startsWith
str.startsWith('pen')
endsWith
str.endsWith('pen')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
startsWith
endsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
258350128.0 Ops/sec
startsWith
207259552.0 Ops/sec
endsWith
226708400.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its options in detail. **Benchmark Overview** The provided benchmark measures the performance of three string methods: `indexOf`, `startsWith`, and `endsWith`. These methods are used to search for a substring within a larger string. The benchmark uses a fixed string `"penpineappleapplepen"` as input, which contains both the target substring ("pen") and its reverse. **Methods Compared** The benchmark compares the performance of three different approaches: 1. **`indexOf` method**: This method returns the index of the first occurrence of the specified substring within the string. 2. **`startsWith` method**: This method returns a boolean indicating whether the string starts with the specified substring. 3. **`endsWith` method**: This method returns a boolean indicating whether the string ends with the specified substring. **Pros and Cons** * **`indexOf` method**: * Pros: Fast, as it only scans the input string once to find the first occurrence of the target substring. * Cons: May return -1 if the target substring is not found, which can be misleading. It also uses a linear search algorithm, making it slower for large strings. * **`startsWith` and `endsWith` methods**: * Pros: More accurate than `indexOf`, as they only check the first or last characters of the string, respectively. They are also faster than `indexOf` for small substrings. * Cons: May be slower for larger substrings due to the overhead of creating a regex pattern. **Library and Purpose** The benchmark uses the JavaScript `String.prototype.indexOf`, `String.prototype.startsWith`, and `String.prototype.endsWith` methods, which are part of the ECMAScript standard. These methods provide a convenient way to search for substrings within strings. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. However, it does utilize some internal optimizations provided by the JavaScript engine, such as: * **String interpolation**: The `str` variable is defined using template literals, which allows for more readable and efficient string concatenation. * **Regex pattern optimization**: Although not explicitly shown in the benchmark code, modern JavaScript engines (including Firefox) use regex patterns internally to optimize performance. **Alternatives** Other alternatives for searching substrings within strings include: * Using a library like Lodash's `str.contains`, which provides more advanced substring search functionality. * Implementing custom substring search algorithms using bitwise operations or regular expressions. * Utilizing third-party libraries like Squirrelfish (a JavaScript performance optimization library) for optimized string searching. Keep in mind that these alternatives may not be as efficient as the built-in methods used by modern JavaScript engines.
Related benchmarks:
indexOf vs substr vs startsWith
index vs lastindexof startsWith
String indexOf vs includes
String indexOf() === 0 vs. startsWith()
Comments
Confirm delete:
Do you really want to delete benchmark?