Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array vs set lookup
(version: 0)
uses array includes, array indexOf, and set has
Comparing performance of:
arr 100 (includes) vs arr 100 (indexOf) vs set 100 vs arr 10000 (includes) vs arr 10000 (indexOf) vs set 10000 vs arr 1000000 (includes) vs arr 1000000 (indexOf) vs set 1000000
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr100 = Array.from({length: 100}, () => Math.floor(Math.random() * 100000)); var set100 = new Set(arr100); //const arr1000 = Array.from({length: 1000}, () => Math.floor(Math.random() * 100000)); //const set1000 = new Set(arr100); var arr10000 = Array.from({length: 10000}, () => Math.floor(Math.random() * 100000)); var set10000 = new Set(arr10000); //const arr100000 = Array.from({length: 100000}, () => Math.floor(Math.random() * 100000)); //const set100000 = new Set(arr100000); var arr1000000 = Array.from({length: 1000000}, () => Math.floor(Math.random() * 100000)); var set1000000 = new Set(arr1000000);
Tests:
arr 100 (includes)
return arr100.includes(1927);
arr 100 (indexOf)
return arr100.indexOf(1927) !== -1;
set 100
return set100.has(1927);
arr 10000 (includes)
return arr10000.includes(1927);
arr 10000 (indexOf)
return arr10000.indexOf(1927) !== -1;
set 10000
return set10000.has(1927);
arr 1000000 (includes)
return arr1000000.includes(1927);
arr 1000000 (indexOf)
return arr1000000.indexOf(1927) !== -1;
set 1000000
return set1000000.has(1927);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
arr 100 (includes)
arr 100 (indexOf)
set 100
arr 10000 (includes)
arr 10000 (indexOf)
set 10000
arr 1000000 (includes)
arr 1000000 (indexOf)
set 1000000
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):
**What is being tested?** MeasureThat.net is testing the performance of three different approaches for lookup operations: `Array.includes()`, `Array.indexOf()`, and `Set.has()`. The test case uses an array of 100, 1,000, 10,000, and 1,000,000 random integers. The benchmark measures the time it takes to perform a lookup operation (i.e., checking if a specific integer is present in the array or set) using each approach. **Options compared** The three options being tested are: 1. `Array.includes()`: This method checks if a value exists in an array by iterating over the elements and returning `true` as soon as the value is found. 2. `Array.indexOf()`: This method returns the index of the first occurrence of a value in an array, or -1 if the value is not found. The test case only measures whether the value is present (i.e., not -1), but still iterates over the entire array to perform this check. 3. `Set.has()`: This method checks if a value exists in a set by using a hash table internally, which allows for fast lookups. **Performance analysis** The benchmark results show that: * `Set.has()` is significantly faster than both `Array.includes()` and `Array.indexOf()` for all array sizes. * `Array.includes()` is generally faster than `Array.indexOf()`, but still slower than `Set.has()`. * The performance difference between `Array.includes()` and `Array.indexOf()` decreases as the array size increases, since the iteration over the entire array in `Array.indexOf()` becomes less efficient. **Conclusion** `Set.has()` is the fastest approach for lookup operations, followed by `Array.includes()`, and then `Array.indexOf()`. This makes sense, given that sets are designed to provide fast lookups using a hash table, while arrays require linear search. The results suggest that using a set data structure can lead to significant performance improvements in certain applications.
Related benchmarks:
set.has vs. array.includes Performance 2
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
set.has vs. array.includes (100000)
Comments
Confirm delete:
Do you really want to delete benchmark?