Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.indexOf > -1 vs String.includes
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
Tests:
indexOf
string.indexOf('commodo') > -1
includes
string.includes('commodo')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Description** The benchmark is designed to compare two different methods for searching a specific substring within a string: `String.indexOf()` and `String.includes()`. The focus is on determining which method performs better in terms of execution speed. **Options Compared** Two main options are being compared: 1. **`string.indexOf('commodo') > -1`**: This uses the traditional `indexOf()` method, which returns the index of the first occurrence of the specified substring. If the substring is not found, it returns `-1`. 2. **`string.includes('commodo')`**: This uses the newer `includes()` method, introduced in ECMAScript 2015 (ES6), which returns a boolean value indicating whether the string includes the specified substring. **Pros and Cons** **`indexOf()` Method:** Pros: * Wide support across older browsers and versions * Simple implementation Cons: * Can be slower due to the need to iterate through the entire string * Returns the index of the first occurrence, not a boolean value indicating presence or absence **`includes()` Method:** Pros: * Generally faster than `indexOf()` * Returns a boolean value indicating presence (true) or absence (false) * More concise and expressive code Cons: * Requires support for ECMAScript 2015 (ES6) and later * May not be as widely supported across older browsers and versions **Library Usage** In the benchmark, both tests use the `string` variable, which is not explicitly mentioned in the JSON. However, we can assume that this string is created using a JavaScript engine or environment that supports string manipulation. **Special JS Feature/Syntax** The `includes()` method uses a new syntax introduced in ECMAScript 2015 (ES6), specifically called "inclusive" matching, which includes the search term itself in the match. This is why `string.includes('commodo')` returns `true`, even if 'commodo' appears only at the end of the string. **Other Alternatives** For the `indexOf()` method, alternative approaches might include: * Using a loop to iterate through the string * Utilizing regular expressions (regex) for more complex matching For the `includes()` method, other alternatives could be: * Manual implementation using the `some()` or `every()` methods with a callback function * Leveraging library functions like `String.prototype.includes.call()` However, it's worth noting that these alternative approaches might not offer significant performance benefits over the built-in `indexOf()` and `includes()` methods. **Benchmark Preparation Code** The provided script preparation code creates a string variable `string` with some sample text. This is likely intended to demonstrate the benchmark scenario without affecting the test results. Overall, the benchmark provides a useful comparison between two JavaScript methods for searching substrings within strings. By understanding the pros and cons of each approach, developers can make informed decisions about when to use `indexOf()` versus `includes()`.
Related benchmarks:
IndexOf vs Includes in Larger string
Javascript index vs substring
Javascript: Case insensitive string comparison performance 3
IndexOf vs Includes in string - larger string edition
RegEx.test vs. String.includes vs. String.match vs String.indexOf - Long String
Comments
Confirm delete:
Do you really want to delete benchmark?