Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active April 11, 2023 06:26
Show Gist options
  • Save artalar/31b4614031b5d30ada92d9d26107acb6 to your computer and use it in GitHub Desktop.
Save artalar/31b4614031b5d30ada92d9d26107acb6 to your computer and use it in GitHub Desktop.
json vs custom
export const random = (min = 0, max = Number.MAX_SAFE_INTEGER - 1) =>
Math.floor(Math.random() * (max - min + 1)) + min
const randomObj = (obj: Record<string, any>, deep: number) => {
if (deep <= 0) return obj
const keys = random(5, 10)
for (let i = 0; i < keys; i++) {
const type = random(0, 2)
if (type === 0) {
obj[`k${random()}`] = random(0, 1) ? new Date() : 1
} else if (type === 1) {
obj[`k${random()}`] = randomObj({}, deep - 1)
} else if (deep === 3) {
obj[`k${random()}`] = Array.from({ length: 10_000 }, () =>
randomObj({}, 2),
)
}
}
return obj
}
const deepClone = (obj: any) => {
if (typeof obj !== 'object' || obj === null) return obj
// if (obj instanceof Date) return new Date(obj)
if (Array.isArray(obj)) {
const res: any[] = []
for (let i = 0; i < obj.length; i++) res[i] = deepClone(obj[i])
return res
}
const res: Record<string, any> = {}
for (const key in obj) res[key] = deepClone(obj[key])
return res
}
const deepCloneTest = (obj: Record<string, any>) => {
console.log('custom clone')
console.time('clone')
const clone = deepClone(obj)
console.timeEnd('clone')
res += Object.keys(clone).length
}
const jsonCloneTest = (obj: Record<string, any>) => {
console.log('JSON.stringify + JSON.parse')
console.time('clone')
const clone = JSON.parse(JSON.stringify(obj))
console.timeEnd('clone')
res += Object.keys(clone).length
}
const structureCloneTest = (obj: Record<string, any>) => {
console.log('structuredClone')
console.time('clone')
const clone = structuredClone(obj)
console.timeEnd('clone')
res += Object.keys(clone).length
}
let i = 5
let res = 0
while (i-- > 2) {
{
const obj = randomObj({}, i)
structureCloneTest(obj)
jsonCloneTest(obj)
deepCloneTest(obj)
console.log(
'complexity:',
JSON.stringify(obj).length.toLocaleString(),
'\n\n',
)
}
{
const obj = randomObj({}, i)
structureCloneTest(obj)
jsonCloneTest(obj)
deepCloneTest(obj)
console.log(
'complexity:',
JSON.stringify(obj).length.toLocaleString(),
'\n\n',
)
}
}
console.log(void res)
@artalar
Copy link
Author

artalar commented Apr 11, 2023

> node -v
v18.15.0

structuredClone
clone: 1.012s
JSON.stringify + JSON.parse
clone: 1.080s
custom clone
clone: 587.651ms
complexity: 30,053,185 


structuredClone
clone: 1.117s
JSON.stringify + JSON.parse
clone: 1.103s
custom clone
clone: 522.192ms
complexity: 25,047,881 


structuredClone
clone: 354.271ms
JSON.stringify + JSON.parse
clone: 426.182ms
custom clone
clone: 258.119ms
complexity: 9,978,403 


structuredClone
clone: 782.17ms
JSON.stringify + JSON.parse
clone: 795.409ms
custom clone
clone: 722.538ms
complexity: 20,067,106 


structuredClone
clone: 0.086ms
JSON.stringify + JSON.parse
clone: 0.052ms
custom clone
clone: 0.021ms
complexity: 957 


structuredClone
clone: 0.027ms
JSON.stringify + JSON.parse
clone: 0.022ms
custom clone
clone: 0.012ms
complexity: 659 
> bun -v
0.5.7
> ReferenceError: Can't find variable: structuredClone

JSON.stringify + JSON.parse
[501.48ms] clone
custom clone
[159.91ms] clone
complexity: 34,947,549 


JSON.stringify + JSON.parse
[375.29ms] clone
custom clone
[110.80ms] clone
complexity: 25,029,361 


JSON.stringify + JSON.parse
[152.33ms] clone
custom clone
[33.39ms] clone
complexity: 10,016,320 


JSON.stringify + JSON.parse
[80.91ms] clone
custom clone
[21.09ms] clone
complexity: 4,988,137 


JSON.stringify + JSON.parse
[0.08ms] clone
custom clone
[0.01ms] clone
complexity: 139 


JSON.stringify + JSON.parse
[0.03ms] clone
custom clone
[0.01ms] clone
complexity: 460 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment