Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep Clone Object: JSON.parse vs Object.assign
(version: 1)
Comparing performance of:
JSON.parse vs Object.assign
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}};
Tests:
JSON.parse
var obj2 = JSON.parse(JSON.stringify(obj));
Object.assign
var obj2 = Object.assign({}, obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.parse
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.parse
2309681.2 Ops/sec
Object.assign
17436006.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Purpose** The benchmark is designed to compare two approaches for creating a deep clone of an object in JavaScript: 1. Using `JSON.parse(JSON.stringify(obj))` 2. Using `Object.assign({}, obj)` **Approaches Comparison** Both approaches aim to create a new, independent copy of the original object `obj`. However, they differ in their implementation: * **JSON.parse(JSON.stringify(obj))**: This method uses the `JSON.stringify()` function to serialize the object and then parses the resulting string back into an object using `JSON.parse()`. The `JSON.stringify()` function can create a deep clone of objects that implement the `JSONSerializable` interface, which includes many built-in JavaScript types like arrays, dates, numbers, etc. * **Object.assign({}, obj)**: This method uses the spread operator (`...`) to create a shallow copy of an object and then uses `Object.assign()` to assign the cloned properties to a new object. While this approach is simple and widely supported, it may not work correctly for nested objects that contain other objects as values. **Pros and Cons** Here are some pros and cons of each approach: * **JSON.parse(JSON.stringify(obj))**: + Pros: Can create deep clones of complex objects, works well with many built-in JavaScript types. + Cons: May not work correctly for objects that don't implement `JSONSerializable`, can be slower due to string serialization. * **Object.assign({}, obj)**: + Pros: Simple and widely supported, works well for shallow copies. + Cons: May not create deep clones of complex objects, may produce unexpected results if the original object has circular references. **Library Used** In this benchmark, no specific library is used. However, `JSON.stringify()` relies on the JSON serializer in your JavaScript implementation, which might be a built-in part of the browser or a separate module. **Special JS Features or Syntax** None are explicitly mentioned in the provided benchmark definition. **Other Alternatives** Other approaches for creating deep clones of objects include: * Using `Object.assign()` with an object that has the same structure as the original object, but uses different variables to create the clone. * Implementing a custom cloning function using recursion or iteration. * Using libraries like Lodash or Immutable.js that provide built-in cloning functions. Keep in mind that these alternatives might have their own pros and cons, which are not detailed here.
Related benchmarks:
oobject clone
Object copy JSON vs Object.assign
Deep cloning of arrays
Deep cloning of arrays with objects
Comments
Confirm delete:
Do you really want to delete benchmark?