Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map doing stuff with clones testtttt
(version: 1)
Comparing performance of:
Set vs Object vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const set = new Set(); for (let i = 0; i < 10000; ++i) { set.add(`key_${i}`); } const obj = {}; for (let i = 0; i < 10000; ++i) { obj[`key_${i}`] = true; } const map = new Map(); for (let i = 0; i < 10000; ++i) { map.set(`key_${i}`, true); }
Tests:
Set
let result = 0; for (let j = 0; j < 100; ++j) { const clone = new Set(set); } console.log(result);
Object
let result = 0; for (let j = 0; j < 100; ++j) { const clone = {...obj}; } console.log(result);
Map
let result = 0; for (let j = 0; j < 100; ++j) { const clone = new Map(map); } console.log(result);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Object
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
87.7 Ops/sec
Object
3.7 Ops/sec
Map
13.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark tests compare the performance of three different data structures in JavaScript: `Set`, `Object`, and `Map`, specifically focusing on their cloning capabilities. Each data structure is instantiated and populated with the same keys, and the benchmark measures the performance of cloning these structures. ### Options Being Compared 1. **Set**: - **Cloning Method**: Creates a new `Set` by passing the existing `Set` to its constructor. - **Test Name**: Set - **Performance**: The execution speed is approximately **35.00 executions per second**. 2. **Object**: - **Cloning Method**: Uses the spread operator `{...obj}` to create a shallow clone of the `Object`. - **Test Name**: Object - **Performance**: The execution speed is approximately **12.80 executions per second**. 3. **Map**: - **Cloning Method**: Creates a new `Map` by passing the existing `Map` to its constructor. - **Test Name**: Map - **Performance**: The execution speed is approximately **30.23 executions per second**. ### Pros and Cons of Each Approach #### Set - **Pros**: - Efficient for storing unique values. - Fast iteration and membership testing. - Cloning with the constructor is straightforward and efficient. - **Cons**: - Limited to storing unique values; cannot have key-value pairs like an object or map. #### Object - **Pros**: - Widely used for key-value pairs and simple data structures. - The spread operator provides a concise way to clone objects. - **Cons**: - Slower performance for cloning compared to `Set` and `Map`. - Cloning with the spread operator only creates a shallow copy, which may lead to issues if the object contains nested structures. #### Map - **Pros**: - Designed for key-value pairs and maintains the insertion order. - Cloning with the constructor is similar to `Set`, making it efficient. - **Cons**: - Slightly slower than `Set`, but generally offers better performance than `Object` for large data sets. ### Considerations - **Use Cases**: The choice among `Set`, `Map`, and `Object` depends on the specific requirements of the application. `Sets` are ideal for collections of unique items, `Maps` are beneficial when order matters or when working with key-value pairs, while `Objects` are suitable for simple data structures. - **Performance Variation**: Performance can vary based on the size of the dataset and the specific operations being performed not just in this benchmark, but also in real-world applications. ### Alternative Approaches Other alternatives for cloning objects might include: - **Deep Copy Libraries**: Libraries like `lodash` provide methods such as `_.cloneDeep()` which perform a deep copy, though they can be slower when clones contain nested structures. - **JSON Methods**: Using `JSON.stringify()` and `JSON.parse()` for cloning objects is another alternative, but it has limitations, such as not supporting functions or special data types (like `Date` or `undefined`). - **Manual Cloning**: Writing a custom cloning function can handle specific requirements (like cloning only certain properties) but increases complexity. These alternatives provide different trade-offs in terms of performance, complexity, and fidelity to the original data structure.
Related benchmarks:
Map clone vs Object clone
Set vs Object vs Map doing stuff
Set vs Object vs Map doing stuff with clones
Set Vs Map Vs Object
Object vs Map (Insert, Delete and Copy)
Map() vs Object() on write
map over Object (or Record in TS) vs Map clone
map vs object test here
Set vs Object vs Map doing stuff with clones using Object.assign()
Comments
Confirm delete:
Do you really want to delete benchmark?