Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
structuredClone vs Object cloning for `Map<number, Set<number>>`
(version: 0)
Comparing performance of:
Object cloning vs structuredClone
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const array_from = Array.from const math_random = Math.random function mapToObject (map) { const obj = {} map.forEach((value, key) => { obj[key] = array_from(value) }) return obj } const randInt = (min, max) => ((math_random() * (max - min) + min) | 0) var map_obj = new Map() const add_random_entry = () => { map_obj.set(randInt(0, 2000), new Set( Array(randInt(0, 20)).fill(0).map( () => randInt(-500, 500) ) )) } for (let i = 0; i < 500; i++) { add_random_entry() }
Tests:
Object cloning
let obj = mapToObject(map_obj) // ensure that jit does not simply jump to disposing of obj obj[12] ? (obj[12][0] += 1) : delete obj[12] obj = undefined
structuredClone
let map_cloned = structuredClone(map_obj) // ensure that jit does not simply jump to disposing of map_cloned map_cloned.get(12) ? (map_cloned.get(12).add(-1)) : (map_cloned.set(12, new Set([5, 5, 5]))) map_cloned = undefined
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object cloning
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares two approaches for cloning a `Map<number, Set<number>>` object in JavaScript: manual object cloning using the `mapToObject()` function and the `structuredClone()` method. **Test Case 1: Object Cloning (Manual)** The first test case creates a `Map<number, Set<number>>` object `map_obj`, populates it with random entries, and then clones it manually using the `mapToObject()` function. The cloned object is used to perform operations on specific keys, ensuring that the JIT compiler does not optimize away the object. **Pros:** * Provides more control over the cloning process * Can be optimized for specific use cases **Cons:** * More complex and error-prone due to manual management of objects * May not work as expected in certain edge cases or with specific JavaScript versions **Test Case 2: structuredClone()** The second test case creates a `Map<number, Set<number>>` object `map_obj`, populates it with random entries, and then clones it using the `structuredClone()` method. The cloned map is used to perform operations on specific keys, ensuring that the JIT compiler does not optimize away the object. **Pros:** * Provides a high-level abstraction for cloning objects * Less error-prone than manual object cloning **Cons:** * May be slower due to the overhead of the `structuredClone()` method * Limited control over the cloning process **Library: structuredClone()** The `structuredClone()` method is a part of the Web APIs specification and is supported in modern browsers, including Firefox 118. It creates a deep copy of an object, including all its properties and children, without modifying the original object. **Special JS Feature/Syntax: None** This benchmark does not use any special JavaScript features or syntax. **Other Alternatives** If `structuredClone()` is not available or desired, other alternatives for cloning objects in JavaScript include: * Using libraries like Lodash or Underscore.js * Implementing a custom cloning function using recursive object traversal * Using the `JSON.parse(JSON.stringify(obj))` method (note: this can be slower and may not work as expected with complex objects) **Benchmark Preparation Code** The provided script preparation code includes: * Initialization of random number generators (`math_random`) and array creation functions (`array_from`) * Creation of a `Map<number, Set<number>>` object `map_obj` with random entries * A loop that adds random entries to the map for performance and reproducibility **Html Preparation Code** No HTML preparation code is provided.
Related benchmarks:
Object vs Map 5
Map vs Object 5000 rand
Map vs object copying
Map vs Object (real-world) Performance - Forked
Comments
Confirm delete:
Do you really want to delete benchmark?