Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Copy map vs. object 10000
(version: 0)
Comparing performance of:
Copy obj vs Copy map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from({ length: 10000 }, (_, i) => ({ id: `item-${i}` })) var dataObj = {}; items.forEach((item, i) => { dataObj[item.id] = { start: i * 30, end: i * 30 + 30 } }); var dataMap = new Map(); items.forEach((item, i) => { dataMap.set(item.id, { start: i * 30, end: i * 30 + 30 }); });
Tests:
Copy obj
const obj = { ...dataObj };
Copy map
const map = new Map(dataMap);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Copy obj
Copy map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 130 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Copy obj
465.1 Ops/sec
Copy map
1771.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance and comparing approaches is an essential part of software development. The provided JSON represents two test cases for measuring the performance difference between copying objects and maps in JavaScript. Here's a breakdown of what's being tested, compared options, pros/cons of each approach, library usage, special JS features, and alternative solutions: **Benchmarked scenarios:** 1. Copying an object (`dataObj`) created using `Array.from()` and iterating over it. 2. Creating a copy of a map (`dataMap`) by setting its entries using the `set()` method. **Comparison options:** There are two approaches to be compared: a. **Object assignment**: Copying the entire `dataObj` object using the spread operator (`const obj = { ...dataObj };`). This creates a shallow copy of the object, which only copies the top-level properties and not nested objects or arrays. b. **Map creation**: Creating a new map by setting its entries using the `set()` method (`const map = new Map(dataMap);`). This creates a deep copy of the map's entries, preserving the original data structure. **Pros and cons:** a. Object assignment: Pros: * Fast and efficient for simple objects with few properties. * Easy to implement. Cons: * Does not preserve nested objects or arrays. * Can lead to shallow copying issues if not used carefully. b. Map creation: Pros: * Preserves the original data structure, including nested objects and arrays. * Handles more complex scenarios efficiently. Cons: * May be slower than object assignment for simple cases due to the overhead of creating a new map. * Requires an additional library (in this case, `Map`) that may not be present in all environments. **Library usage:** The test uses the built-in `Array.from()` and `Map` data structures. The `Map` class is used specifically for copying the entries. **Special JS features:** There are no special JavaScript features or syntax being tested in this benchmark. Both object assignment and map creation use standard JavaScript methods and APIs. **Alternative solutions:** 1. **Lodash's `cloneDeep()` function**: This function can be used to create a deep copy of objects and arrays, preserving the original data structure. 2. **JSON-based approaches**: Using JSON serialization (e.g., `JSON.parse(JSON.stringify(dataObj)))` or deserialization (`JSON.parse()` on a copied map) could provide a simple way to create copies, but these methods may not be as efficient as the built-in object and map creation APIs. When choosing between these approaches, consider the specific use case, data structure complexity, and performance requirements.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Object vs map 100
Copy map vs. object
Comments
Confirm delete:
Do you really want to delete benchmark?