Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deep freeze vs deep clone
(version: 0)
can be used in similar ways when you want immutable data
Comparing performance of:
deepFreeze vs deepClone
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function deepClone (obj) { const result = {} for (const key in obj) { const value = obj[key] if (value !== null && typeof value === 'object') { result[key] = deepClone(value) } else { result[key] = value } } return result } function deepFreeze (obj) { for (const key in obj) { const value = obj[key] if (value !== null && typeof value === 'object') { if (Object.isFrozen(value) === false) { deepFreeze(value) } } } return Object.freeze(obj) } var source = { foo: 'foo', bar: 'bar', baz: 'baz', qux: { foo: 'foo', bar: 'bar', baz: 'baz', qux: { foo: 'foo', bar: 'bar', baz: 'baz', qux: { foo: 'foo', bar: 'bar', baz: 'baz', qux: 'qux' } } } }
Tests:
deepFreeze
deepFreeze(source)
deepClone
deepClone(source)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
deepFreeze
deepClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
deepFreeze
5828569.5 Ops/sec
deepClone
754112.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's being tested in the provided JSON, describe the different approaches, their pros and cons, and other considerations. **What is being tested?** The benchmark compares two approaches to create immutable data objects: `deepFreeze` and `deepClone`. The test case uses an object `source` as input. Both functions are designed to create a new, independent copy of the original object, ensuring that any modifications made to the cloned object do not affect the original. **Deep Freeze vs Deep Clone** ### Deep Freeze `deepFreeze` is a function that takes an object as input and returns a frozen version of it using `Object.freeze()`. The purpose of this approach is to create an immutable copy of the original object, making it difficult for other parts of the program to modify its properties. Pros: * Efficient: Creating a new, frozen object is relatively fast compared to deep cloning. * Immutable: The resulting object cannot be modified directly. Cons: * Unfreezable: Once created, the frozen object cannot be unfrozen or modified using standard JavaScript methods. * Recursive issues: If the original object contains circular references (i.e., objects that reference each other), `deepFreeze` may not work correctly. ### Deep Clone `deepClone`, on the other hand, creates a new, independent copy of the original object by recursively traversing its properties and creating new objects for each value. This approach ensures that any modifications made to the cloned object do not affect the original. Pros: * Immutable: The resulting object cannot be modified directly. * Recursive support: Handles circular references in the original object. Cons: * Inefficient: Deep cloning can be slower than `deepFreeze` due to the overhead of creating new objects for each property. * More complex: The cloned object has a separate memory allocation, which may lead to increased garbage collection and memory usage. **Library Used:** Neither `deepFreeze` nor `deepClone` rely on any external libraries. They are pure JavaScript functions that work directly with the standard library. **Special JS Feature/Syntax:** No special JavaScript features or syntax are used in these benchmark cases. The focus is solely on the efficiency and correctness of the two approaches to creating immutable data objects. **Other Alternatives:** If you need an even more efficient solution, consider using `Object.seal()` or `Object.freeze()` with a shim library like `es6-promise` (for older browsers) to ensure recursive support for `deepFreeze`. Alternatively, you can use third-party libraries like Lodash's `cloneDeep()` function for deep cloning. However, these alternatives may introduce additional dependencies and complexity, making them less suitable for simple benchmarking exercises.
Related benchmarks:
Object Deep Copy with deep clone
deep freeze vs deep clone (version 2)
JSON.stringify vs Deep Clone
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?