Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs startsWith vs includes
(version: 0)
Comparing the string methods: indexOf, startsWith, includes
Comparing performance of:
startsWith vs indexOf vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
startsWith
const url = "https://example.com/path/to/index.html" url.startsWith("https://example.com")
indexOf
const url = "https://example.com/path/to/index.html" url.indexOf("https://example.com") > -1
includes
const url = "https://example.com/path/to/index.html" url.includes("https://example.com")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
indexOf
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
37290164.0 Ops/sec
indexOf
137541904.0 Ops/sec
includes
133043168.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a benchmark test that compares the performance of three string methods in JavaScript: `indexOf`, `startsWith`, and `includes`. The test creates a sample URL string, then uses each method to check if the URL starts with or contains a specific substring (`"https://example.com"`). **Options Compared** The benchmark tests the following options: 1. **`indexOf()`**: This method searches for the specified substring in the original string and returns its index. 2. **`startsWith()`**: This method checks if the original string starts with the specified substring. 3. **`includes()`**: This method checks if the original string includes the specified substring. **Pros and Cons of Each Approach** 1. **`indexOf()`**: * Pros: Efficient for searching a specific substring, as it uses a linear search algorithm. * Cons: May be slower than other methods for large strings or when searching for substrings that are not present in the string. 2. **`startsWith()`**: * Pros: Faster than `indexOf()` for small strings, as it only needs to check the first few characters. * Cons: May be slower than `includes()` for larger strings, as it still performs a search operation. 3. **`includes()`**: * Pros: More efficient than `indexOf()` or `startsWith()` when searching for substrings in large strings, as it uses a more optimized algorithm. * Cons: May be slower than `startsWith()` for small strings, as it needs to perform a full search. **Library Usage** There is no explicit library usage mentioned in the benchmark. However, some JavaScript engines may provide additional methods or optimizations for these string operations. For example, Chrome's V8 engine has implemented various optimizations for string operations, including `startsWith()` and `includes()`. **Special JS Features/Syntax** None of the test cases explicitly use any special JavaScript features or syntax, such as async/await, Promises, or modern ES modules. The benchmark focuses on comparing the performance of basic string methods. **Other Alternatives** If you wanted to modify this benchmark, here are some alternative options: * Test other string methods, such as `endsWith()`, `replace()`, or `split()`. * Use different input strings or substrings to see how the performance compares. * Modify the test code to include additional logic or operations that affect performance, such as regular expressions or string concatenation. * Compare the performance of these methods on different types of data, such as numbers, arrays, or objects.
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?