Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs JSON.parse(JSON.stringify()) 2
(version: 0)
Benchmarks spread vs JSON.parse(JSON.stringify())
Comparing performance of:
Spread vs Parse & Stringify vs Stringify
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
var theJSON = { "squadName": "Super hero squad", "homeTown": "Metro City", "formed": 2016, "secretBase": "Super tower", "active": true, "members": [ { "name": "Molecule Man", "age": 29, "secretIdentity": "Dan Jukes", "powers": [ "Radiation resistance", "Turning tiny", "Radiation blast" ] }, { "name": "Madame Uppercut", "age": 39, "secretIdentity": "Jane Wilson", "powers": [ "Million tonne punch", "Damage resistance", "Superhuman reflexes" ] }, { "name": "Eternal Flame", "age": 1000000, "secretIdentity": "Unknown", "powers": [ "Immortality", "Heat Immunity", "Inferno", "Teleportation", "Interdimensional travel" ] } ] } var result = {...theJSON}
Parse & Stringify
var theJSON = { "squadName": "Super hero squad", "homeTown": "Metro City", "formed": 2016, "secretBase": "Super tower", "active": true, "members": [ { "name": "Molecule Man", "age": 29, "secretIdentity": "Dan Jukes", "powers": [ "Radiation resistance", "Turning tiny", "Radiation blast" ] }, { "name": "Madame Uppercut", "age": 39, "secretIdentity": "Jane Wilson", "powers": [ "Million tonne punch", "Damage resistance", "Superhuman reflexes" ] }, { "name": "Eternal Flame", "age": 1000000, "secretIdentity": "Unknown", "powers": [ "Immortality", "Heat Immunity", "Inferno", "Teleportation", "Interdimensional travel" ] } ] } var result = JSON.parse(JSON.stringify(theJSON))
Stringify
var theJSON = { "squadName": "Super hero squad", "homeTown": "Metro City", "formed": 2016, "secretBase": "Super tower", "active": true, "members": [ { "name": "Molecule Man", "age": 29, "secretIdentity": "Dan Jukes", "powers": [ "Radiation resistance", "Turning tiny", "Radiation blast" ] }, { "name": "Madame Uppercut", "age": 39, "secretIdentity": "Jane Wilson", "powers": [ "Million tonne punch", "Damage resistance", "Superhuman reflexes" ] }, { "name": "Eternal Flame", "age": 1000000, "secretIdentity": "Unknown", "powers": [ "Immortality", "Heat Immunity", "Inferno", "Teleportation", "Interdimensional travel" ] } ] } var result = JSON.stringify(theJSON)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread
Parse & Stringify
Stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **What is being tested?** The benchmark tests three different approaches to create a shallow copy of an object in JavaScript: 1. **Spread syntax**: Creating a new object by spreading the properties of the original object using the `...` operator. 2. **JSON.parse(JSON.stringify())**: Using the `JSON.parse()` function with `JSON.stringify()` as its argument to recreate the object. 3. **Stringify**: Using the `JSON.stringify()` function to serialize the object and then parsing it back into a new object. **Options compared** The benchmark compares the performance of these three approaches in terms of: * Executions per second (in this case, on an iPhone device running iOS 17.1.1) **Pros and cons of each approach:** 1. **Spread syntax**: * Pros: Creates a new object with the same properties as the original, and is concise. * Cons: May not work correctly if the object has complex references (e.g., circular references) or non-enumerable properties. 2. **JSON.parse(JSON.stringify())**: * Pros: Can handle complex objects, including circular references and non-enumerable properties. * Cons: Can be slower due to the additional parsing step required by `JSON.parse()`. 3. **Stringify**: (Note that this approach is not a traditional way of creating a copy of an object in JavaScript) * Pros: May work correctly even with complex objects, but may also introduce more overhead than necessary. * Cons: Can be slower due to the additional stringification and parsing steps. **Other considerations** * The `JSON.parse(JSON.stringify())` approach can fail if the original object contains functions or other non-enumerable properties, as these will not be properly copied. * The spread syntax approach may be more prone to errors if the original object has complex references (e.g., circular references) or non-enumerable properties. **Benchmark results** The latest benchmark results show that: 1. Spread syntax performs well with an average execution rate of 5294861 executions per second. 2. JSON.parse(JSON.stringify()) performs slightly worse, with an average execution rate of 354555.03125 executions per second. 3. Stringify (note: not a traditional copy approach) has the lowest performance, with an average execution rate of 293045.96875 executions per second. Keep in mind that these results are specific to the iPhone device and iOS 17.1.1 environment, and may vary depending on other factors such as system resources and JavaScript engine optimizations.
Related benchmarks:
Spread vs JSON stringify
JSON.parse vs string.split
JSON.stringify VS Number.toString
JSON.parse vs string.splitd
JSON.parse vs string.splitds
Comments
Confirm delete:
Do you really want to delete benchmark?