Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
aadasdas
(version: 0)
a
Comparing performance of:
JSON parse vs Object assign vs Structured clone
Created:
one year ago
by:
Guest
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);
Structured clone
var obj2 = structuredClone(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
JSON parse
Object assign
Structured clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON parse
1575900.6 Ops/sec
Object assign
6605411.5 Ops/sec
Structured clone
772632.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data to understand what's being tested and compared. **Benchmark Definition** The provided benchmark definition represents an object `obj` with several properties: `a`, `c`, `po`, `arr`, and another nested object `anotherObj`. The script preparation code is a JavaScript expression that creates a new copy of the object using different methods. There are three test cases: 1. **JSON Parse**: This test case uses the `JSON.parse()` function to parse the string representation of the original object `obj`. 2. **Object Assign**: This test case uses the `Object.assign()` method to create a shallow copy of the original object `obj`. 3. **Structured Clone**: This test case uses the `structuredClone()` function (a relatively new feature introduced in JavaScript) to create a deep clone of the original object `obj`. **Comparison** The three methods being compared are: * **JSON Parse**: Creates a new object by parsing the string representation of the original object. This method does not preserve the object's prototype chain, and it can be slower due to the additional parsing step. * **Object Assign**: Creates a new object by iterating over the properties of the original object and copying them. This method preserves the object's prototype chain but may not work correctly if the original object has cyclic references or complex relationships between its properties. * **Structured Clone**: Creates a deep clone of the original object, preserving all its properties, including nested objects and arrays, as well as their prototype chains. **Pros and Cons** Here are some pros and cons for each method: * **JSON Parse**: + Pros: Easy to implement, fast execution time. + Cons: Does not preserve prototype chain, can be slower due to parsing step. * **Object Assign**: + Pros: Preserves prototype chain, relatively fast execution time. + Cons: May not work correctly with cyclic references or complex relationships between properties. * **Structured Clone**: + Pros: Creates deep clone, preserves all properties and their prototype chains. + Cons: Relatively new feature, may require additional support from browsers. **Library and Special JS Features** No libraries are used in this benchmark. However, the `structuredClone()` function is a relatively new feature introduced in JavaScript, which allows creating deep clones of objects. **Other Alternatives** If you need to clone an object in JavaScript, other alternatives include: * Using a library like Lodash's `cloneDeep` or `merge` * Implementing a custom cloning function using recursion or iteration * Using the `JSON.parse()` method with `JSON.stringify()` to create a deep copy (although this method is not as efficient as structured clone) Keep in mind that the choice of cloning method depends on your specific use case and performance requirements.
Related benchmarks:
Object speard vs assign
Test method Object
typeof first or second
Lodash get
Obj vs Arr Js raed
Comments
Confirm delete:
Do you really want to delete benchmark?