Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs. JSON String/Parse
(version: 2)
Creating a "new" object reference every time
Comparing performance of:
Spread operator vs JSON Parse vs Object.assign
Created:
8 years ago
by:
Registered User
Jump to the latest result
Tests:
Spread operator
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 = [...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
Spread operator
JSON Parse
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread operator
30858646.0 Ops/sec
JSON Parse
67949432.0 Ops/sec
Object.assign
70707736.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and test cases. **Benchmark Definition:** MeasureThat.net is testing three different approaches to create a new object reference: 1. `Object.assign()`: This function creates a shallow copy of an existing object by merging all its enumerable properties into a new object. 2. JSON String/Parse: This approach uses the `JSON.parse()` and `JSON.stringify()` methods to convert an object to a JSON string, which can then be parsed back into an object. The test aims to measure the performance difference between these three approaches in creating a new object reference every time. **Options Compared:** * **Object.assign()**: This is a built-in JavaScript method that creates a shallow copy of an existing object. + Pros: Fast, efficient, and widely supported. + Cons: Only creates a shallow copy, which means it only copies the top-level properties of the original object. It also modifies the original object if necessary. * **JSON String/Parse**: This approach converts the object to a JSON string using `JSON.stringify()` and then parses it back into an object using `JSON.parse()`. + Pros: Creates a deep copy of the original object, which means all properties and nested objects are copied recursively. It also does not modify the original object. + Cons: Can be slower than Object.assign() due to the stringification and parsing process. **Other Considerations:** * The test uses a while loop to repeat the creation of new object references until a condition is met (in this case, 1000 iterations). * The test does not account for any potential side effects or memory allocation issues that may arise from creating large numbers of objects. * The results are likely affected by factors such as JavaScript engine optimizations, hardware, and operating system. **Library Usage:** None of the benchmark definitions use external libraries or frameworks. However, it's worth noting that some browsers may have additional features or APIs that can affect the performance of these tests (e.g., `Object.assign()` might be optimized in certain browsers). **Special JS Feature/Syntax:** There is no special JavaScript feature or syntax used in these benchmark definitions. **Alternatives:** * Other approaches to create a new object reference could include: + Using the spread operator (`{...obj}`) instead of `Object.assign()`. + Using a library like Lodash's `cloneDeep()` function. + Implementing a custom deep copy algorithm using recursion or iteration. Keep in mind that these alternatives might not be as widely supported or optimized as Object.assign(), and their performance may vary depending on the specific use case and environment.
Related benchmarks:
Deep Clone Object: JSON.parse vs Object.assign
Object.assign vs direct copy
Object copy JSON vs Object.assign
Object.assign vs spreading object copy
Object.assign vs mutation
Comments
Confirm delete:
Do you really want to delete benchmark?