Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs array.indexOf
(version: 0)
Comparing performance of:
includes vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(10000).fill(undefined).map((_, i) => i);
Tests:
includes
return a.includes(9)
indexOf
return a.indexOf(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net! The provided JSON represents two benchmark test cases: **Benchmark Definition** The `Name` field indicates that we're comparing the performance of `array.includes()` and `array.indexOf()`. The `Description` is empty, which means there's no descriptive text for this benchmark. **Script Preparation Code** This code snippet creates an array `a` with 10,000 elements, filling them with `undefined`, and then maps over it to generate indices (`i`). This will be used as the input array for our benchmarks. **Html Preparation Code** There's no HTML preparation code provided, which means we're not generating any specific HTML structure to run our benchmarks on. Now, let's break down the individual test cases: 1. **Benchmark Definition: "return a.includes(9)"** This line of code checks if the number 9 exists within the array `a`. The `includes()` method searches for the specified value (`9`) in the array and returns `true` if found. 2. **Benchmark Definition: "return a.indexOf(9)"** This line of code attempts to find the index of the number 9 within the array `a`. The `indexOf()` method returns the index of the first occurrence of the specified value (`9`) or -1 if not found. **What are we comparing?** We're comparing the performance ( execution time) of two different approaches: 1. **`array.includes()`**: This method searches for a single element in the array and returns `true` if found, otherwise `false`. It's generally faster than `indexOf()`. 2. **`array.indexOf()`**: This method attempts to find the index of a specific element in the array. If the element is not found, it returns -1. **Pros and Cons** * **`array.includes()`**: + Pros: Generally faster than `indexOf()`, as it can return immediately if the element is found. + Cons: May perform an unnecessary iteration over the entire array if the element is not found. * **`array.indexOf()`**: + Pros: Can provide a meaningful index value even if the element is not found. + Cons: Generally slower than `includes()`, as it needs to iterate over the entire array to find the first occurrence. **Library and Special Features** Neither of these benchmark test cases uses any external libraries or special JavaScript features. They're focused solely on comparing the performance of the two built-in methods. **Other Alternatives** If you want to explore other approaches, here are a few alternatives: * **`array.prototype.includes()`**: This is the recommended method in modern JavaScript (ES6+). It's generally faster than `includes()`, but may have slightly different behavior if used with older browsers. * **`binarySearch()`**: If you're willing to implement your own binary search algorithm, it can be significantly faster than the built-in methods. However, this approach requires additional logic and is not as straightforward. Keep in mind that these alternative approaches might change depending on the specific requirements of your project or use case. That's a summary of the JavaScript microbenchmarking example on MeasureThat.net!
Related benchmarks:
IndexOf vs includes js (idanlevi1)
IndexOf vs Includes array of numbers
Array find with indexOf vs includes
includes indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?