Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
small set vs array lookup
(version: 1)
Comparing performance of:
array vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2]; var b = new Set(a)
Tests:
array
const items = [1, 2, 3] const pick = items[Math.floor(Math.random() * items.length)]; a.includes(pick)
set
const items = [1, 2, 3] const pick = items[Math.floor(Math.random() * items.length)]; b.has(pick)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
49175380.0 Ops/sec
set
119919184.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the given benchmark "small set vs array lookup," the performance of two different data structures in JavaScript is being tested: an array and a Set. The benchmark aims to evaluate the speed and efficiency of lookups in these two structures when performing a random selection from a predefined set of items. ### Benchmark Setup 1. **Preparation Code**: - An array `a = [1, 2]` is defined. - A Set `b` is created from the array `a` using `new Set(a)`. This converts the array into a Set containing the same values. ### Test Cases Two test cases are executed: 1. **Array Lookup**: - Test Name: **array** - Benchmark Definition: ```javascript const items = [1, 2, 3] const pick = items[Math.floor(Math.random() * items.length)]; a.includes(pick) ``` - This code randomly selects a value from the `items` array and checks for its presence in the array `a` using the `includes` method. 2. **Set Lookup**: - Test Name: **set** - Benchmark Definition: ```javascript const items = [1, 2, 3] const pick = items[Math.floor(Math.random() * items.length)]; b.has(pick) ``` - This code also randomly selects a value from the `items` array but checks for its presence in the Set `b` using the `has` method. ### Performance Results - The benchmark results show that the `Set` lookup (executions per second: 119,919,184) is significantly faster than the array lookup (executions per second: 49,175,380). This indicates that checking membership in a Set is generally more efficient than in an array for the given data. ### Pros and Cons #### Array (`a.includes()`) - **Pros**: - Simplicity: Arrays are easy to use and widely understood by developers. - Order: Arrays maintain the order of elements, which can be useful in certain scenarios. - **Cons**: - Performance: The `includes` method has a time complexity of O(n) because it needs to check each element in the array sequentially. - Duplicates: Arrays can contain duplicate values, which may not be desirable in all use cases. #### Set (`b.has()`) - **Pros**: - Performance: The `has` method in a Set has a time complexity of O(1) on average, making it much faster for lookups than arrays. - Uniqueness: Sets automatically enforce uniqueness, preventing duplicate entries and helping maintain a clean dataset. - **Cons**: - Memory Overhead: Sets can consume more memory than arrays, especially for small datasets. - Limited Functionality: Sets don’t have the same range of methods as arrays (e.g., no direct access by index). ### Other Considerations - **Use Cases**: The choice between using an array and a Set should depend on the specific needs of the application. If frequent membership checks are needed, a Set would be more appropriate. For maintaining order or expecting a collection of items, arrays might be more applicable. - **Alternatives**: Other alternatives to these structures include: - **Maps**: Useful for key-value pairing and when needing to store relationships. - **Objects**: Can serve as associative arrays for storing key-value pairs. In conclusion, this benchmark highlights the performance differences between arrays and Sets in JavaScript, particularly in the context of checking for the existence of elements. The test clearly favors the Set implementation for its efficiency, making it a preferable choice in scenarios where lookup speed is critical.
Related benchmarks:
test filtering with set
set.has vs. array.includes large array
Set.has vs. Array.includes
set.has vs. array.includes performance
includes vs has 20220126
set.has vs. array.includes Performance 2
set.has vs. array.includes 10k el
set.has vs. array.includes (100000)
small set vs array lookup 2
Comments
Confirm delete:
Do you really want to delete benchmark?