Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf on big data
(version: 0)
Comparing performance of:
includes vs lookup vs indexOf
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = []; for(let i = 0; i < 10000; i++){ a.push(i) } var b = new Set(a)
Tests:
includes
return a.includes(9567)
lookup
return b.has(9567)
indexOf
return a.indexOf(9567)!== -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
26464788.0 Ops/sec
lookup
27682446.0 Ops/sec
indexOf
953520.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. The benchmark is designed to compare the performance of three different approaches for checking if an element exists in an array: `includes()`, `has()` from the `Set` object, and `indexOf()`. **Includes()** This approach uses the `includes()` method on the `array` variable. This method returns `true` if an element with the specified value is found in the array, and `false` otherwise. Pros: * Simple to implement * Works for most use cases Cons: * Can be slower than other approaches for large datasets due to the overhead of iterating through the entire array. * May not be optimized for performance, especially on older browsers or with very large arrays. **Has() from Set** This approach uses the `has()` method on the `Set` object created from the `array`. This method returns `true` if the specified value is found in the set, and `false` otherwise. Pros: * Can be faster than `includes()` for large datasets since sets use a hash table for storage, allowing for O(1) lookups. * Optimized for performance and can take advantage of CPU caching. Cons: * Requires creating a new set object from the array, which can incur additional overhead. * May not work as expected if the original array contains duplicate values or if the `Set` object is modified externally. **IndexOf()** This approach uses the `indexOf()` method on the `array` variable. This method returns the index of the first occurrence of the specified value in the array, and `-1` if the value is not found. Pros: * Can be faster than `includes()` for large datasets since it only needs to iterate through a portion of the array to find the element. * Optimized for performance and can take advantage of CPU caching. Cons: * Returns the index of the first occurrence, which may not be what the developer intended (e.g., if there are multiple occurrences). * May not work as expected if the original array is modified externally. **Library: Set** The `Set` object is a built-in JavaScript data structure that stores unique values in an ordered set. Its primary purpose is to provide efficient membership testing, which is exactly what the `has()` method does. Pros: * Fast and optimized for performance * Suitable for most use cases Cons: None mentioned. **Special JS feature: None** There are no special JavaScript features or syntaxes being used in this benchmark. Now, let's consider other alternatives to these approaches. Other methods for checking if an element exists in an array include: * `Array.prototype.every()` with a callback function that returns `true` if the element is found * Using a loop and manual iteration through the array * Using a library like Lodash or Ramda, which provide optimized functions for membership testing However, these alternatives may not be as efficient or optimized as the approaches being compared in this benchmark. Keep in mind that the choice of approach depends on the specific use case and performance requirements. In general, `includes()` is a safe choice for most cases, while `has()` from the `Set` object provides a faster alternative for large datasets. `IndexOf()` can be useful if you need to find the index of an element, but it may not be what the developer intended in all cases.
Related benchmarks:
set.has vs. array.includes large
set.has vs. array.includes large 1
set.has vs. array.includes large 3
set.has vs. array.includes (1 million entries)
set.has vs. array.includes - large array
Comments
Confirm delete:
Do you really want to delete benchmark?