Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 22333
(version: 1)
Comparing performance of:
includes vs lookup
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(1000000).fill(undefined).map((_, i) => i); 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:
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
110336568.0 Ops/sec
lookup
202945296.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares the performance of two different methods for checking the presence of a value in a collection: using an array with the `includes` method versus using a `Set` with the `has` method. Here’s a closer look at what is being tested, the options compared, their pros and cons, and other relevant considerations. ### Benchmark Overview 1. **Data Preparation**: - **Array (`a`)**: An array of one million undefined values is created, which is then populated with integers from 0 to 999,999 using the `map` function. - **Set (`b`)**: A `Set` is created from the previously populated array. A `Set` is a built-in object in JavaScript that allows you to store unique values of any type. 2. **Test Cases**: - **Test Case 1 - `includes`**: Tests the presence of the integer `9` in the array using the `Array.prototype.includes` method. - **Test Case 2 - `lookup`**: Tests the presence of the integer `9` in the `Set` using the `Set.prototype.has` method. ### Performance Metrics The benchmark results indicate that the `has` method of the `Set` operates significantly faster than the `includes` method of the array: - `lookup` (Set.has(9)) achieved approximately **202,945,296 executions per second**. - `includes` (Array.includes(9)) achieved approximately **110,336,568 executions per second**. ### Pros and Cons of Each Approach #### Using Array with `includes` **Pros**: - Simplicity: The syntax is straightforward and easy to understand. - Good for small datasets where performance differences may be negligible. **Cons**: - Linear Time Complexity: The operation `array.includes` has O(n) time complexity because it may need to iterate through all elements in the worst case until it finds the target or reaches the end. - Performance Degradation: As the size of the array increases, the time to check for the presence of an element can become significantly slower. #### Using Set with `has` **Pros**: - Fast Lookup: The `Set.prototype.has` method is optimized for performance with O(1) average time complexity, resulting in faster lookups regardless of the size of the set. - Uniqueness: A `Set` automatically handles uniqueness, so you don’t have to worry about duplicate values. **Cons**: - Memory Overhead: A `Set` might consume more memory than a standard array, as it stores its data structurally to allow for fast lookups. - Slightly More Complex: Requires use of a different data structure which might be less familiar to some developers. ### Additional Considerations When deciding which method to use, developers should take into account the size of the dataset and the frequency of search operations. For small arrays or infrequent checks, using the array with `includes` might be perfectly sufficient. However, for larger or dynamic datasets where membership checks are frequent, using a `Set` with `has` is much more efficient. ### Alternatives 1. **Object Lookup**: In JavaScript, you can also use objects as maps for checking key existence (`obj.hasOwnProperty(key)`), though this approach does not support duplicate keys and may involve some overhead in JavaScript engines dealing with prototype chains. 2. **Map**: If key-value pairs are needed, a `Map` object might be used, which also provides O(1) average complexity for key lookups. Understanding the differences, pros, and cons of these methods can significantly impact performance in scenarios where membership testing is critical, making it an important consideration in your JavaScript development practices.
Related benchmarks:
set.has vs. array.includes
set.has vs. array.includesdd
set.has vs. array.includes (100 items)
set.has vs. array.includes (10000 items)
set.has vs. array.includes (large: 100)
set.has vs. array.includes (large: 10000)
set.has vs. array.includes on 25 000 items
set.has vs. array.includes2222adgag
set.has vs. array.includes (0)
set.has vs. array.includes for 1000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?