Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS string includes vs indexOf
(version: 0)
JS string includes vs indexOf
Comparing performance of:
String.prototype.includes vs String.prototype.indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = ''; var i = 0; while (i <= 1E5) str += i++; str += 'end';
Tests:
String.prototype.includes
const item = str.includes('end');
String.prototype.indexOf
const index = str.indexOf('end');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.prototype.includes
String.prototype.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.prototype.includes
168954736.0 Ops/sec
String.prototype.indexOf
162151600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The benchmark measures the performance difference between two string searching methods in JavaScript: `includes()` and `indexOf()`. The test case creates a large string by concatenating numbers from 0 to 100,000, followed by the word "end". This allows the benchmark to evaluate the efficiency of these string searching methods under various conditions. **Options compared** Two options are being compared: 1. **String.prototype.includes()**: This method returns `true` if the string contains the specified value, and `false` otherwise. It is typically used with substrings or values that can be present in a string without necessarily matching it exactly. 2. **String.prototype.indexOf()**: This method returns the index of the first occurrence of the specified value in the string, or `-1` if not found. **Pros and cons** * **includes():** + Pros: Efficient for searching substrings or values that can be present in a string without matching it exactly. + Cons: May have performance issues when dealing with very large strings or exact matches, as it uses a linear search algorithm. However, this is often not an issue in practice, and the method is generally faster than `indexOf()` for these use cases. * **indexOf():** + Pros: Suitable for searching exact matches or finding the first occurrence of a value in a string. + Cons: May have performance issues when dealing with very large strings, as it uses a linear search algorithm. However, this method is generally faster than `includes()` when an exact match is expected. **Library usage** There is no specific library used in this benchmark. The two methods are part of the JavaScript Standard Library. **Special JS features or syntax** None are mentioned in the provided benchmark definition. **Other considerations** When interpreting these results, consider the following: * The benchmark is designed to evaluate the performance difference between `includes()` and `indexOf()`, which may not be a critical concern for most use cases. * The test string size (100,000) might not accurately represent real-world scenarios. Larger strings would likely exhibit different performance characteristics. * Browser and environment variations can impact performance results. **Alternative approaches** If you're looking to create your own benchmark or optimize the performance of these methods in a specific scenario, consider the following alternatives: 1. **Use a more optimal string searching algorithm**: For large strings, algorithms like Boyer-Moore or Knuth-Morris-Pratt might be more efficient. 2. **Use a specialized library**: Depending on your use case, libraries like `jsstrlib` or `fast-string-search` provide optimized implementations for string searching methods. 3. **Profiling and optimization techniques**: Use tools like Chrome DevTools or Node.js Inspector to identify performance bottlenecks in your application and apply optimizations accordingly. Keep in mind that the best approach depends on the specific requirements of your use case and performance characteristics of your target environment.
Related benchmarks:
index vs lastindexof startsWith
Js Search -String IndexOf vs Includes
index vs lastindexof (last index)
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?