Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endswith vs indexof
(version: 0)
Comparing performance of:
endsWith vs indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "http://www.example.com/path/command.html"
Tests:
endsWith
str.endsWith('command.html');
indexOf
str.indexOf('command.html') != -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
endsWith
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endsWith
35106324.0 Ops/sec
indexOf
112032824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark, named "endswith vs indexof", compares the performance of two JavaScript methods: `str.endsWith('command.html')` and `str.indexOf('command.html') != -1`. Both methods are used to check if a string contains a specific substring ("command.html") but differ in their approach. **Methods Being Compared** 1. **endsWith**: This method returns a boolean value indicating whether the string ends with the specified substring. 2. **indexOf**: This method returns the index of the first occurrence of the specified substring, or -1 if it's not found. **Pros and Cons of Each Approach** **endsWith:** Pros: * More efficient for strings that end with the desired substring (e.g., "http://www.example.com/path/command.html" -> "command.html") * Can be optimized by browser engines to avoid unnecessary string comparisons Cons: * May not work correctly for substrings that are part of a larger sequence (e.g., "abc123command.html") **indexOf:** Pros: * Works with any substring, regardless of its position in the original string * Can be more flexible when dealing with complex search patterns Cons: * Typically slower than `endsWith` due to the need to scan the entire string for the substring * May not be optimized as well by browser engines, leading to slower performance **Other Considerations** Both methods have a time complexity of O(n), where n is the length of the input string. However, modern JavaScript engines and browsers have implemented various optimizations, such as caching results or using lookup tables, which can significantly impact performance. **Library Used (None)** There are no external libraries used in this benchmark. **Special JS Feature/Syntax (None)** This benchmark does not use any special JavaScript features or syntax. **Alternatives** For a more comprehensive benchmarking platform, you might want to explore other tools like: 1. jsbench.net 2. Benchmark.js 3. Speedtest.js These libraries provide more advanced features, such as automated test case generation, support for multiple programming languages, and integration with popular frameworks like React or Angular. **Benchmark Preparation Code** The provided Script Preparation Code sets the input string `str` to `"http://www.example.com/path/command.html"`. The HTML Preparation Code is empty, indicating that no additional setup is required for this benchmark.
Related benchmarks:
index vs lastindexof startsWith
String indexOf vs startsWith/endsWith
startsWith vs indexOf vs endsWith
indexOf vs includes search2
Comments
Confirm delete:
Do you really want to delete benchmark?