Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Js Search - String StartsWith vs IndexOf vs Includes
(version: 0)
Comparing performance of:
Starts With vs Index Of vs Includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Starts With
var a = ';
Index Of
var a = ';
Includes
var a = ';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Starts With
Index Of
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Starts With
81869360.0 Ops/sec
Index Of
538896192.0 Ops/sec
Includes
51012824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark measures the performance of three string search methods in JavaScript: `startsWith`, `indexOf`, and `includes`. These methods are commonly used to check if a substring is present within a larger string. **Benchmark Definition** The JSON data defines two test cases: 1. "Starts With" 2. "Index Of" 3. "Includes" Each test case involves creating a string (`a`) with a sample Lorem ipsum text and then checking if another string (`b`) starts with, contains as an index, or includes the specified substring. **Options Compared** The benchmark compares three different string search methods: 1. **`startsWith`**: Returns `true` if the string `b` starts with the specified substring. 2. **`indexOf`**: Returns the index of the first occurrence of the specified substring in the string `a`. If the substring is not found, returns `-1`. 3. **`includes`**: Returns `true` if the string `a` includes the specified substring. **Pros and Cons** * **`startsWith`**: * Pros: More efficient than `indexOf` or `includes`, especially when searching for a specific prefix. * Cons: May not be as intuitive to use, as it requires careful consideration of edge cases (e.g., empty strings). * **`indexOf`**: * Pros: Simple to use and understand, with minimal overhead. Suitable for most use cases where the search is not exact. * Cons: Can be slower than `startsWith`, especially when searching for a specific prefix or substring in a large string. * **`includes`**: * Pros: More readable and intuitive than `indexOf`, with fewer edge cases to consider. Suitable for most use cases where the search is exact. * Cons: May be slower than `startsWith` due to the overhead of searching for the substring in the entire string. **Library Usage** None of the benchmark test cases explicitly use a JavaScript library. However, it's worth noting that some libraries (e.g., Lodash) provide optimized implementations of these methods. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** Other string search methods in JavaScript include: * `localeCompare`: Compares two strings based on their locale and language settings. * `match`: Returns an array of matches for a regular expression applied to the string. * `replace`: Replaces occurrences of a specified pattern in the string with a new value. Keep in mind that each of these methods has its own strengths, weaknesses, and use cases. This benchmark provides a concise way to compare the performance of three common string search methods: `startsWith`, `indexOf`, and `includes`. If you're interested in exploring other options or learning more about the trade-offs between these methods, I can provide additional resources or guidance!
Related benchmarks:
String indexOf vs startsWith/endsWith
Js Search -String IndexOf vs Includes
Js Search - String StartsWith vs Includes
String indexOf() === 0 vs. startsWith()
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?