Last active
April 11, 2023 06:26
-
-
Save artalar/31b4614031b5d30ada92d9d26107acb6 to your computer and use it in GitHub Desktop.
json vs custom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
> 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