Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object lookup
(version: 0)
Comparing performance of:
Object vs Set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var object = {}; var array = new Array(5000).fill().map((_, i) => i); for (let i = 0; i < array.length; i++) { object[i] = true; } for (let i = 0; i < array.length; i++) { set.add(i); }
Tests:
Object
object[40]
Set
set.has(40)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:130.0) Gecko/130.0 Firefox/130.0
Browser/OS:
Firefox Mobile 130 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
1166710016.0 Ops/sec
Set
1161168000.0 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 analyzed. **What is being tested:** The benchmark compares two approaches for looking up elements in different data structures: 1. **Object lookup**: The benchmark tests the performance of accessing an element at a specific index using bracket notation (`object[40]`) on a large object. 2. **Set lookup**: The benchmark tests the performance of checking if an element is present in a set using the `has()` method (`set.has(40)`) on a large set. **Options compared:** The two options being compared are: 1. **Object bracket notation** (`object[40]`): This approach uses the bracket notation to access an element at a specific index in the object. 2. **Set has() method** (`set.has(40)`): This approach uses the `has()` method to check if an element is present in the set. **Pros and Cons of each approach:** 1. **Object bracket notation**: * Pros: + Fast access times, as it directly accesses the underlying array. + Can be more convenient for accessing elements at specific indices. * Cons: + May require more memory to store the large object. + May have performance issues if the object is not properly optimized (e.g., using sparse arrays). 2. **Set has() method**: * Pros: + More memory-efficient, as sets only store unique values and do not allocate space for indices. + Can be more convenient for checking membership in a set of elements. * Cons: + May have slower access times compared to bracket notation, especially for large datasets. + May require additional overhead for creating and managing the set. **Library and its purpose:** The `Set` library is used to create and manipulate sets. A set is an unordered collection of unique values that can be added, removed, or checked for membership. In this benchmark, the `Set` library is used to create a large set with 5000 elements, which are then checked using the `has()` method. **Special JS feature or syntax:** None mentioned in the provided code. However, it's worth noting that modern JavaScript engines often use various optimizations and caching mechanisms under the hood, such as array indexing cacheing, to improve performance for common operations like bracket notation access. **Other alternatives:** Other data structures that could be used for this benchmark include: * **Array indices**: Similar to object bracket notation, but using an array index instead of an object. * **Map**: A map is a similar data structure to an object, but it uses keys and values instead. Accessing elements in a map using the `has()` method would be more efficient than using bracket notation. * **Hash tables**: Hash tables are optimized for fast lookups and insertion/deletion operations, making them suitable alternatives to sets for this benchmark. These alternative approaches would require adjustments to the script preparation code and might not offer significant performance differences for small datasets. However, they could provide insight into the trade-offs between different data structures and access patterns.
Related benchmarks:
Set array expansion Part 2
creating maps vs creating objects
Set vs Object add
allocating objects vs allocating maps
Comments
Confirm delete:
Do you really want to delete benchmark?