Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object vs Map doing stuff with clones
(version: 0)
Comparing performance of:
Set vs Object vs Map
Created:
4 years ago
by:
Guest
Go to the latest result
Tests:
Set
const set = new Set(); for (let i = 0; i < 1000; ++i) { set.add(`key_${i}`); } let result = 0; for (let j = 0; j < 100; ++j) { const clone = new Set(set); for (let k = -500; k < 500; ++k) { result += clone.has(`key_${k}`); } } console.log(result);
Object
const obj = {}; for (let i = 0; i < 1000; ++i) { obj[`key_${i}`] = true; } let result = 0; for (let j = 0; j < 100; ++j) { const clone = {...obj}; for (let k = -500; k < 500; ++k) { result += clone.hasOwnProperty(`key_${k}`); } } console.log(result);
Map
const map = new Map(); for (let i = 0; i < 1000; ++i) { map.set(`key_${i}`, true); } let result = 0; for (let j = 0; j < 100; ++j) { const clone = new Map(map); for (let k = -500; k < 500; ++k) { result += clone.get(`key_${k}`); } } 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:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Set
199/s
Map
190/s
Object
133/s
View exact numbers
Test name
Executions per second
✓
Set
199 Ops/sec
Map
190 Ops/sec
Object
133 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the world of JavaScript benchmarking. **What is being tested?** The provided JSON represents a benchmark that compares the performance of three different data structures in JavaScript: `Set`, `Object`, and `Map`. Specifically, each test case measures how long it takes to perform a certain operation on a cloned version of these data structures. The operations are: 1. **Adding elements**: Creating a clone of the data structure and checking if an existing key exists. 2. **Checking existence**: Cloning the data structure and iterating over its elements to check if a specific key is present. **What options are compared?** The three test cases compare the performance of `Set`, `Object`, and `Map` in handling these operations: 1. **Test case "Set"**: Uses a `Set` data structure, which is an unordered collection of unique values. 2. **Test case "Object"**: Uses a plain JavaScript object (`{}`), which can store key-value pairs with some limitations (e.g., duplicate keys are not allowed). 3. **Test case "Map"**: Uses a `Map` data structure, which is an ordered collection of key-value pairs. **Pros and cons of each approach:** 1. **Set**: Pros: * Fast lookup times (`has()` method) since it uses a hash table internally. * Good for storing unique values. * Lightweight implementation. Cons: * Not designed for storing key-value pairs (e.g., no `get()`, `set()`, or `delete()` methods). 2. **Object**: Pros: * Familiar interface, easy to use for developers already comfortable with JavaScript objects. * Supports key-value pairs and other object operations. Cons: * Not designed for storing unique values; duplicate keys are allowed but may lead to unexpected behavior. * Lookup times can be slower than `Set` or `Map` since it uses a prototype-based lookup mechanism. 3. **Map**: Pros: * Designed specifically for key-value pairs, providing fast and efficient operations (`get()`, `set()`, `delete()`). * Supports iteration over entries. Cons: * May have additional overhead due to its ordered nature (e.g., insertion order is preserved). **Library usage:** None of the test cases use a specific library. The benchmarks rely solely on built-in JavaScript data structures (`Set`, `Object`, and `Map`). **Special JS feature or syntax:** The only special JavaScript feature used in this benchmark is the spread operator (`{...obj}`), which creates a shallow copy of an object's properties. **Other alternatives:** For similar use cases, you may also consider: 1. **WeakMap**: A type of `Map` that uses weak references to its keys and values. 2. **HashMap**: An implementation of a hash map data structure, often used in JavaScript libraries or frameworks. 3. **Array.prototype.reduce()**: Can be used to simulate a map operation on an array. Keep in mind that the results may vary depending on your specific use case, library versions, and browser/platform environment.
Related benchmarks
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Object.assign() vs Reflect.set()
Comments
Confirm delete:
Do you really want to delete benchmark?