Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map v Object v Set v Array memory overhead
(version: 3)
Comparing performance of:
map vs set vs obj vs arr vs WeakMap vs WeakSet
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var map = new Map(); var set = new Set(); var obj = {}; var arr = []; var weakMap = new WeakMap(); var weakSet = new WeakSet(); const a = Array.from({ length: 1000 }); var vals = a.map(v => Math.random() * 1000); var valsString = a.map(v => String(Math.random() * 1000)); var valsObj = a.map(v => { return { v: Math.random() * 1000 } });
Tests:
map
vals.forEach(va => map.set(va, va))
set
vals.forEach(va => set.add(va))
obj
valsString.forEach(va => obj[va] = va)
arr
valsString.forEach(va => arr.push(va))
WeakMap
valsObj.forEach(va => weakMap.set(va, va))
WeakSet
valsObj.forEach(va => weakSet.add(va))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
map
set
obj
arr
WeakMap
WeakSet
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map
14941.4 Ops/sec
set
17678.7 Ops/sec
obj
8265.0 Ops/sec
arr
29471.9 Ops/sec
WeakMap
14042.8 Ops/sec
WeakSet
14815.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, which compares the performance of different data structures: `Map`, `Set`, `WeakMap`, and `Array`. The benchmark is designed to measure the memory overhead and execution time of each data structure. **Test Cases and Data Structures** Each test case has a unique name and a corresponding Benchmark Definition. The main difference between these definitions is the way they interact with their respective data structures: 1. **map**: The benchmark defines a `Map` instance and iterates over an array using `forEach`, setting each element to itself in the map. 2. **set**: Similar to the `map` test, but uses a `Set` instead of a `Map`. 3. **obj**: Iterates over an array of strings using `forEach`, storing each string as a property in an object. 4. **arr**: Iterates over the same array of strings using `forEach`, pushing each string onto an array. 5. **WeakMap** and **WeakSet**: Similar to the `map` and `set` tests, but use `WeakMap` and `WeakSet` respectively. **Options Compared** The benchmark compares the performance of different data structures under various operations: * Iteration over arrays using `forEach` * Setting elements to themselves in a map * Storing strings as properties in an object * Pushing strings onto an array These options are compared to understand their relative performance and memory usage. **Pros and Cons** Here's a brief summary of the pros and cons for each data structure: 1. **Map**: * Pros: Fast lookups, efficient iteration. * Cons: More memory overhead due to the use of keys. 2. **Set**: * Pros: Fast membership testing, efficient iteration. * Cons: Less flexible than `Map` in terms of key-value relationships. 3. **WeakMap** and **WeakSet**: * Pros: Weak reference handling, less memory overhead compared to regular Maps and Sets. * Cons: Can lead to performance issues if the underlying objects are not properly cleaned up. **Library Usage** None of the data structures use external libraries in this benchmark. **Special JS Features/Syntax** The benchmark uses the following special JavaScript features: 1. **WeakMaps** and **WeakSets**: These are designed for handling weak references, which can be beneficial for garbage collection purposes. 2. `forEach`: This method is used for iterating over arrays and objects. Other Considerations * The benchmark uses an array of 1000 elements to simulate a large dataset. * The use of `Array.from` creates an array with the desired length. * The use of `Math.random()` generates random values for demonstration purposes. **Alternatives** If you're looking for alternative data structures or libraries, consider: 1. **Ordered Maps**: Some libraries, like Lodash, offer ordered maps that provide faster lookups and iteration compared to regular maps. 2. **Tree-based Data Structures**: For certain use cases, tree-based data structures like Binary Search Trees (BSTs) can be more efficient than arrays or maps for data storage and retrieval. Keep in mind that the choice of data structure ultimately depends on the specific requirements of your project, such as performance, memory usage, or flexibility.
Related benchmarks:
Array vs Object vs Map vs WeakMap access3
Map vs WeakMap (real-world) Performance
WeakMap vs arrays read performance !!
Map v Object v Set v Array ALL base actions
Comments
Confirm delete:
Do you really want to delete benchmark?