Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes large array
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const NUM_ITEMS = 1000000; var a = []; for(let i = 1; i < NUM_ITEMS; i++) { a.push(i); } var b = new Set(a)
Tests:
includes
const NUM_ITEMS = 1000000; return a.includes(Math.random() * NUM_ITEMS)
lookup
const NUM_ITEMS = 1000000; return b.has(Math.random() * NUM_ITEMS)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
1088.9 Ops/sec
lookup
2238841.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and its pros and cons. **Benchmark Overview** The test compares two approaches to check if an element exists in a large array: `set.has` (using a Set data structure) versus `array.includes` (a built-in method of arrays). **Script Preparation Code** The script prepares a large array `a` with 1,000,000 elements and creates a new Set `b` containing the same elements. This is done to create a scenario where both methods can be applied. ```javascript const NUM_ITEMS = 1000000; var a = []; for (let i = 1; i < NUM_ITEMS; i++) { a.push(i); } var b = new Set(a); ``` **Comparison** The test case is divided into two parts: 1. `includes`: Returns `true` if the array contains an element with the given value. 2. `lookup`: Similar to `includes`, but using the `has` method of the Set data structure. Both methods are expected to be called with a random index between 1 and 1,000,000. **Pros and Cons** * **set.has** + Pros: In general, `has` is more efficient than `includes` for large datasets since it only needs to check if the element exists in the set. This can lead to faster performance. + Cons: If the array contains duplicate elements, this can lead to slower performance due to the additional overhead of checking for duplicates. * **array.includes** + Pros: More intuitive and widely supported than `has`. It can handle arrays with duplicate elements more efficiently since it uses a linear search algorithm. + Cons: Generally slower than `has` for large datasets, especially if the array is very large. **Other Considerations** * **Memory Usage**: Both methods have similar memory usage since they both store references to the elements in question. However, sets typically use less memory overall due to their more compact representation of data. * **Cache Performance**: The `includes` method might benefit from cache-friendly access patterns in modern JavaScript engines, making it potentially faster than `has` for certain workloads. **Library and Special JS Feature** No specific libraries or special JavaScript features are used beyond the standard array, Set, and Math functions.
Related benchmarks:
set.has vs. array.includes (1 million entries)
set vs array iteration 100k elements
set vs array iteration many
set vs array iteration new new
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?