Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs includes with substring in string
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
IndexOf vs Includes
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = "https://portobellomarina.com/favicon.ico"
Tests:
IndexOf
array.indexOf('favicon.ico') !== 1
Includes
array.includes('favicon.ico')
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 explain what's being tested. **Benchmark Overview** The test measures the performance difference between two ways to check if an array contains a value: `indexOf` and `includes`. The input is a URL string that represents the favicon.ico image from the website portobellomarina.com. **Options Compared** There are two options being compared: 1. **indexOf**: This method returns the index of the first occurrence of the specified value in the array, or -1 if it's not found. 2. **includes**: This method returns a boolean indicating whether the array includes the specified value. **Pros and Cons** * `indexOf`: + Pros: It can be more efficient when searching for specific values that are expected to be present in the array. + Cons: If the value is not found, it can lead to negative index values or exceptions if not handled properly. * `includes`: + Pros: It's a safer option that returns a boolean value directly, making it easier to handle edge cases. It also doesn't require the value to be present in the array for an efficient search. + Cons: It may be slower than `indexOf` when searching for specific values. **Library Used** The benchmark uses the Lodash library (`lodash.js`) as a dependency, which provides the `includes` method. The library's purpose is to provide functional programming utilities and helpers, such as array manipulation and string operations. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It only utilizes built-in methods and libraries like Lodash. **Other Alternatives** If you wanted to test the performance of `indexOf` without using Lodash, you could use the native `Array.prototype.indexOf()` method provided by JavaScript engines. However, since this is already implemented in most browsers, it's unlikely that a significant performance difference would be observed. In general, when benchmarking performance, it's essential to consider factors like: * Cache locality and reuse * Branch prediction and misprediction penalties * Instruction-level parallelism (ILP) and pipelining * Memory access patterns and cache hierarchy Keep in mind that the results of this benchmark may not be representative of all use cases or scenarios.
Related benchmarks:
IndexOf vs Includes
Substring in a string: IndexOf vs Includes vs lodash includes
array indexOf vs includes vs findIndex
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?