Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs weakset
(version: 0)
Comparing performance of:
set vs weak set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var objs = Array(100).fill().map(() => { return {} }) var obj = objs[50] var set = new Set() var weakset = new WeakSet() var times = 100 while (times--) { set.add(objs[times]) weakset.add(objs[times]) }
Tests:
set
set.has(obj)
weak set
weakset.has(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
weak set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
62059076.0 Ops/sec
weak set
79336336.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition:** The benchmark is designed to compare the performance of two data structures in JavaScript: `Set` and `WeakSet`. The script preparation code creates an array of 100 objects, adds these objects to both sets, and then measures how quickly each set can check if a specific object is present using the `has()` method. **Options Compared:** Two options are compared: 1. **Set**: A regular JavaScript `Set` data structure. 2. **WeakSet**: A weak reference implementation of a `Set` data structure, which allows garbage collection to occur even if an object is no longer referenced elsewhere in the code. **Pros and Cons:** **Regular Set (Set)** Pros: * Fast lookup times due to its hash-based implementation * No overhead from garbage collection * Can handle arbitrary JavaScript objects Cons: * Does not account for memory leaks or circular references, which can lead to performance issues **WeakSet** Pros: * Automatically releases memory when an object is no longer referenced elsewhere in the code * Handles circular references and memory leaks more efficiently than a regular Set * Still provides fast lookup times due to its hash-based implementation Cons: * May have slower performance due to garbage collection overhead * Requires careful management of object lifetimes to avoid unnecessary garbage collection **Library:** In this benchmark, the `WeakSet` library is not explicitly mentioned. However, it's worth noting that WeakSets are a built-in JavaScript feature since ECMAScript 2011 (ES6). They provide a way to create weak references to objects, which can help prevent memory leaks and circular references. **Special JS Feature/Syntax:** This benchmark does not use any special JavaScript features or syntax. It's a straightforward test of the performance of `Set` and `WeakSet`. **Alternatives:** If you're interested in exploring alternative data structures for JavaScript, consider: 1. **Object**: A JavaScript object is not typically used as a data structure for fast lookups, but it can be used with techniques like caching or hashing to improve performance. 2. **Map**: A `Map` data structure provides fast key-value lookups and can be more efficient than a Set for certain use cases. 3. **Custom Data Structures**: You can create custom data structures using JavaScript classes, objects, and arrays to optimize performance for specific use cases. In summary, the MeasureThat.net benchmark is designed to compare the performance of regular `Set` and weakly-referenced `WeakSet` data structures in JavaScript. Understanding the pros and cons of each approach will help you choose the best solution for your specific use case.
Related benchmarks:
Object.defineProperty vs Object.assign vs. Direct property assignment
WeakMap vs arrays read performancesadsasd
WeakMap vs arrays read performance (fixed)
Map vs WeakMap (real-world) Performance
WeakMap vs arrays read performance !!
Comments
Confirm delete:
Do you really want to delete benchmark?