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}
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))
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)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Parse & Stringify | |
Stringify |
Test name | Executions per second |
---|---|
Spread | 5294861.0 Ops/sec |
Parse & Stringify | 354555.0 Ops/sec |
Stringify | 293046.0 Ops/sec |
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:
...
operator.JSON.parse()
function with JSON.stringify()
as its argument to recreate the object.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:
Pros and cons of each approach:
JSON.parse()
.Other considerations
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.Benchmark results
The latest benchmark results show that:
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.