Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes Performance
(version: 0)
set.has vs. array.includes Performance
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 100000 }, (_, el) => el); var b = new Set(a);
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down the benchmark definition and test results. **Benchmark Definition** The benchmark is called "set.has vs. array.includes Performance". This suggests that we're comparing the performance of two different methods: `set.has()` and `array.includes()`. Both methods are used to check if a specific value exists within a data structure (a Set or an Array, respectively). **Script Preparation Code** The script preparation code is: ```javascript var a = Array.from({ length: 100000 }, (_, el) => el); var b = new Set(a); ``` This code creates two data structures: * An array `a` with 100,000 elements (all integers from 0 to 99,999). * A set `b` created from the array `a`. This means that any duplicate values in the array will be removed, leaving a unique collection of integers. **Individual Test Cases** There are two test cases: 1. **"includes"**: The benchmark definition is: "return a.includes(9)". This tests how fast it is to use the `array.includes()` method on array `a` to check if the value 9 exists in the array. 2. **"lookup"**: The benchmark definition is: "return b.has(9)". This tests how fast it is to use the `set.has()` method on set `b` to check if the value 9 exists in the set. **Latest Benchmark Results** The results show two measurements: 1. For test case "lookup": * Browser: Chrome 107 * Device Platform: Desktop * Operating System: Mac OS X 10.15.7 * Executions Per Second (EPS): 153,070,180.0 2. For test case "includes": * Browser: Chrome 107 * Device Platform: Desktop * Operating System: Mac OS X 10.15.7 * Executions Per Second (EPS): 14,828,668.0 **Comparison and Analysis** The results show that the `set.has()` method ("lookup") is significantly faster than the `array.includes()` method ("includes"). This makes sense because sets in JavaScript are optimized for fast lookup operations, while arrays are more general-purpose data structures. **Pros and Cons of Set vs. Array** * **Set**: Pros: Fast lookup times (average O(1) time complexity), easy to use for unique collections. Cons: Cannot store duplicate values. * **Array**: Pros: Can store arbitrary data types and duplicate values, flexible data structure. Cons: Lookup operations can be slower (average O(n) time complexity). **Other Alternatives** If you need to check if a value exists in an array with many duplicates, you could consider using a `Map` instead of a Set or Array. A Map is similar to a Set but allows for arbitrary key-value pairs. However, this would require more code changes and might not be the best solution depending on your specific use case. In summary, if you need fast lookup operations and can tolerate unique collections, a Set is likely the better choice. If you need to store duplicate values or have a more complex data structure, an Array (or other alternative) might be more suitable.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes 2
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?