Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs. JSON String/Parse vs deepclone
(version: 0)
Creating a "new" object reference every time
Comparing performance of:
deepclone vs JSON Parse vs Object.assign
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
deepclone
function deepClone(obj) { if (obj === null || typeof obj !== 'object') { return obj; } const clone = Array.isArray(obj) ? [] : {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { clone[key] = deepClone(obj[key]); } } return clone; } var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]}; while (n.length < 1000) { n = deepClone(n); }
JSON Parse
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]}; while(n.length < 1000) { n = JSON.parse(JSON.stringify(n)) }
Object.assign
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]}; while(n.length < 1000) { n = Object.assign({}, n); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
deepclone
JSON Parse
Object.assign
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
deepclone
38459496.0 Ops/sec
JSON Parse
64580096.0 Ops/sec
Object.assign
97911848.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark. **Benchmark Definition** The test is designed to compare three approaches for creating a new object reference: 1. `Object.assign()` 2. JSON string parsing 3. Deep cloning using a custom function (`deepClone()`) The benchmark aims to measure which approach creates the most efficient and scalable way to create a large number of objects. **Options being compared** The options being compared are: * `Object.assign()`: A method that creates a new object by copying properties from an existing object. * JSON string parsing: This involves creating a new object from a JSON string. * Deep cloning using a custom function (`deepClone()`): A recursive function that creates a deep copy of an object. **Pros and Cons** Here's a brief overview of each approach: 1. `Object.assign()`: * Pros: Fast, widely supported, and easy to use. * Cons: Creates a shallow copy of the original object, which may not be suitable for complex data structures like arrays or objects with nested properties. 2. JSON string parsing: * Pros: Can handle complex data structures like arrays and objects with nested properties. * Cons: Slower than `Object.assign()` due to the overhead of parsing the JSON string. 3. Deep cloning using a custom function (`deepClone()`): * Pros: Creates a deep copy of the original object, which is suitable for complex data structures. * Cons: Slow and may not be optimized for performance. **Library usage** The `JSON.parse()` function uses the built-in `JSON` library in JavaScript. This library provides methods for parsing JSON data from strings to JavaScript objects. **Special JS feature or syntax** There are no special JS features or syntaxes used in this benchmark that would require additional explanation. **Other alternatives** If you're looking for alternative approaches, consider: 1. Using the `JSON.parse()` method with a custom parser to handle complex data structures. 2. Implementing a custom deep cloning algorithm using recursive function calls. 3. Using a library like Lodash or Ramda to provide a deep clone function. However, keep in mind that these alternatives may introduce additional overhead and complexity compared to the original benchmark. **Benchmark result interpretation** The latest benchmark results show that `Object.assign()` is the fastest approach, followed closely by JSON string parsing and then deep cloning using the custom `deepClone()` function. This suggests that for this specific use case, `Object.assign()` is the most efficient way to create a new object reference.
Related benchmarks:
Deep Clone Object: JSON.parse vs Object.assign
Object.assign vs direct copy
Lodash cloneDeep vs JSON Clone vs fastDeepClone
JSON.stringify vs Deep Clone
Comments
Confirm delete:
Do you really want to delete benchmark?