Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes() vs Array.indexOf()
(version: 0)
Comparing performance of:
Array.includes() vs Array.indexOf()
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
Tests:
Array.includes()
var result = array.includes('d');
Array.indexOf()
var result = array.indexOf('d') > -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes()
Array.indexOf()
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 what's being tested in this benchmark. **What is being tested?** The benchmark compares the performance of two methods: `array.includes()` and `array.indexOf()`. Both methods are used to search for a specific value ("d") within an array. **Options compared:** 1. **`array.includes('d')`**: This method checks if the specified value (`'d'`) exists in the array using the `includes()` method. It returns a boolean value indicating whether the value is present or not. 2. **`array.indexOf('d') > -1`**: This method searches for the specified value (`'d'`) in the array and returns its index if found, or `-1` if not found. **Pros and Cons of each approach:** * `array.includes('d')`: + Pros: More concise and readable syntax. It's a one-line expression that directly returns a boolean value. + Cons: Might be slower in some cases, as it uses a binary search algorithm under the hood. * `array.indexOf('d') > -1`: + Pros: Can return the actual index of the found element if needed, and might be faster for large arrays with unique values. + Cons: Requires an additional comparison (`> -1`) to get the boolean result. The syntax is more verbose. **Library used in the test case:** None explicitly mentioned, but `array` is a built-in JavaScript array object. **Special JS feature or syntax:** There are no specific special features or syntaxes being tested in this benchmark. However, it's worth noting that both methods rely on the JavaScript engine's implementation of arrays and the `includes()` method. **Other alternatives:** For searching for values in an array, other methods might include: * Using a `for` loop to iterate through the array. * Using the `some()` method, which returns `true` if at least one element matches the condition. * Using the `findIndex()` method, which returns the index of the first element that matches the condition. Keep in mind that these alternatives might have different performance characteristics and use cases compared to the `includes()` and `indexOf()` methods.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes array of numbers
IndexOf vs Includes in array
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?