Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Assing vs JSON
(version: 0)
Comparing performance of:
JSON vs Assign vs Spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
JSON
var obj = {a:'foo',b:'bar'}; var obj2 = JSON.parse(JSON.stringify(obj));
Assign
var obj = {a:'foo',b:'bar'}; var obj2 = Object.assign({}, obj);
Spread
var obj = {a:'foo',b:'bar'}; var obj2 = {...obj};
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
JSON
Assign
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON
3062369.0 Ops/sec
Assign
20276866.0 Ops/sec
Spread
52304412.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three ways to create a shallow copy of an object: using `JSON.parse(JSON.stringify(obj))`, `Object.assign({}, obj)`, and the spread operator (`{...obj}`). The goal is to determine which method is the fastest. **Library Used: None** There are no external libraries used in this benchmark. All tests rely on built-in JavaScript features. **Special JS Features/Syntax** The benchmark uses a feature called "Spread Syntax" (introduced in ECMAScript 2018) to create a shallow copy of an object using the syntax `{...obj}`. **Test Cases** There are three test cases: 1. **JSON**: Creates a deep copy of an object using `JSON.parse(JSON.stringify(obj))`. 2. **Assign**: Creates a shallow copy of an object using `Object.assign({}, obj)`. 3. **Spread**: Creates a shallow copy of an object using the spread syntax `{...obj}`. **Pros and Cons of Each Approach** 1. **JSON**: This method creates a deep copy of the entire object, including its nested objects. While it can be convenient for some cases, it's slower than the other two methods because of the overhead of parsing JSON. * Pros: Creates a complete copy of the original object, including nested objects. * Cons: Slower due to JSON parsing and creation of additional objects. 2. **Assign**: This method creates a shallow copy of the object, copying only its top-level properties. It's faster than the `JSON` method but can lead to issues if the original object has mutable nested objects. * Pros: Faster than `JSON` method, suitable for most use cases with simple objects. * Cons: Can be problematic with mutable nested objects, as it creates references instead of copies. 3. **Spread**: This method uses the spread syntax to create a shallow copy of an object. It's fast and efficient but may not work correctly if the original object has complex or non-standard property names. * Pros: Fastest of the three methods, suitable for most simple objects with basic properties. * Cons: Can be problematic with complex or non-standard property names, as it only copies top-level properties. **Other Alternatives** If these methods don't suit your needs, you can consider using other techniques to create shallow copies of objects: 1. **`Object.assign()` with an array**: `Object.assign([], obj)` creates a new array and assigns the object's values to it. 2. **Using `lodash.cloneDeep()` or other library functions**: If you need more complex copying (e.g., preserving prototypes), consider using libraries like Lodash, which provide `cloneDeep()` and other utility functions. Keep in mind that each method has its trade-offs, and the best approach depends on your specific use case and requirements.
Related benchmarks:
Lodash cloneDeep vs JSON Clone vs Object Spread
Lodash cloneDeep vs structuredClone vs JSON Parse (deep object)
Plain Json: lodash clonedeep vs json.parse(stringify())
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify but with big data
deep json spread vs flat
Comments
Confirm delete:
Do you really want to delete benchmark?