Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array includes vs Array.indexOf big
(version: 0)
Comparing performance of:
Array.includes vs Array.indexOf
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.includes
let size = 1000000 let markets = [] for (let i = 1; i<size; i++) { markets.push(Math.floor(Math.random() * size)); } markets.includes(49);
Array.indexOf
let size = 1000000 let markets = [] for (let i = 1; i<size; i++) { markets.push(Math.floor(Math.random() * size)); } markets.indexOf(49) > 0;
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
4.5 Ops/sec
Array.indexOf
4.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition JSON** The benchmark definition is a simple JavaScript function that creates an array of 1 million random numbers, then checks if a specific number (49) is included in the array using two different methods: `Array.includes` and `Array.indexOf`. **Options Compared** Two options are compared: 1. **Array.includes**: This method returns `true` if the specified value is present in the array, and `false` otherwise. 2. **Array.indexOf**: This method returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. **Pros and Cons** * **Array.includes**: + Pros: Simple to implement, fast for large arrays (because it uses a hash table lookup), and provides a concise way to check if an element is present. + Cons: May be slower than `Array.indexOf` for small arrays or when the value is not present, because it has to iterate through the array. * **Array.indexOf**: + Pros: Can return the index of the first occurrence of the specified value, which can be useful in certain scenarios. It's also generally faster than `Array.includes` for small arrays or when the value is present. + Cons: Returns -1 if the value is not found, which can be slower than returning a boolean value with `Array.includes`. Also, it has to iterate through the array, making it less efficient for large arrays. **Library and Special Features** There are no libraries used in these benchmark cases. However, note that the use of `Math.floor` suggests that the benchmark is testing the behavior of floating-point arithmetic in JavaScript. **Other Considerations** The benchmark only tests two specific scenarios: finding a value in an array using `Array.includes` or `Array.indexOf`. To get a more comprehensive understanding of these methods, additional test cases could be added to cover other edge cases, such as: * Finding multiple values in the array * Searching for a value at a specific index * Handling null or undefined values **Alternatives** Other alternatives to `Array.includes` and `Array.indexOf` include: * Using `Set` data structure, which provides faster membership tests for unique elements. * Implementing custom lookup tables or hash functions for arrays. * Utilizing native WebAssembly (WASM) APIs for optimized array operations. Keep in mind that these alternatives might not be as widely supported or well-documented as the standard `Array.includes` and `Array.indexOf` methods.
Related benchmarks:
IndexOf vs Includes array of numbers
JavaScript Benchmark: includes vs indexOf
IndexOf vs Includes in array
Array find with indexOf vs includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?