Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active December 22, 2023 13:46
Show Gist options
  • Save JobLeonard/1d276a826f1b4c3eddd42ef85b1ece90 to your computer and use it in GitHub Desktop.
Save JobLeonard/1d276a826f1b4c3eddd42ef85b1ece90 to your computer and use it in GitHub Desktop.
JSON.parse(JSON.stringify()) vs structuredClone()
{"title":"JSON.parse(JSON.stringify()) vs structuredClone()","initialization":"// runs once\n\nconst bigArray = [];\nfor (let i = 0; i < 1000; i++) {\n bigArray.push({\n title: (Math.random() * 0x1000000 | 0).toString(32) + (Math.random() * 0x1000000 | 0).toString(32) + (Math.random() * 0x1000000 | 0).toString(32),\n attendees: [(Math.random() * 0x1000000 | 0).toString(32), \"Steve\", (Math.random() * 0x1000000 | 0).toString(32)]\n });\n}","setup":"// runs before each test","tests":[{"name":"JSON.parse(JSON.stringify(bigArray))","code":"const clone = JSON.parse(JSON.stringify(bigArray))","results":{"aborted":false,"count":128,"cycles":3,"hz":1648.8251301030457,"stats":{"moe":0.000007082888943024567,"rme":1.1678445282987906,"sem":0.000003613718848481922,"deviation":0.000028909750787855375,"mean":0.0006064924543802311,"variance":8.357736906159045e-10,"numSamples":64},"times":{"cycle":0.07763103416066958,"elapsed":5.814,"period":0.0006064924543802311,"timeStamp":1703252753987}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0":{"aborted":false,"count":168,"cycles":5,"hz":2072.075544420889,"stats":{"moe":0.000003420634569974414,"rme":0.7087813238844647,"sem":0.000001745221719374701,"deviation":0.000013961773754997608,"mean":0.0004826078869047623,"variance":1.9493112638574002e-10,"numSamples":64},"times":{"cycle":0.08107812500000006,"elapsed":5.974,"period":0.0004826078869047623,"timeStamp":1703252723161}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36":{"aborted":false,"count":128,"cycles":3,"hz":1648.8251301030457,"stats":{"moe":0.000007082888943024567,"rme":1.1678445282987906,"sem":0.000003613718848481922,"deviation":0.000028909750787855375,"mean":0.0006064924543802311,"variance":8.357736906159045e-10,"numSamples":64},"times":{"cycle":0.07763103416066958,"elapsed":5.814,"period":0.0006064924543802311,"timeStamp":1703252753987}}}},{"name":"structuredClone(bigArray)","code":"const clone = structuredClone(bigArray);","results":{"aborted":false,"count":79,"cycles":3,"hz":1004.8128543004093,"stats":{"moe":0.000011348602464623988,"rme":1.140322163479949,"sem":0.0000057901032982775445,"deviation":0.00004703902156201612,"mean":0.0009952101983172177,"variance":2.2126695495118173e-9,"numSamples":66},"times":{"cycle":0.0786216056670602,"elapsed":5.897,"period":0.0009952101983172177,"timeStamp":1703252759806}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0":{"aborted":false,"count":75,"cycles":2,"hz":903.4388964445309,"stats":{"moe":0.000021387019130372142,"rme":1.9321864961381479,"sem":0.000010911744454271501,"deviation":0.00008591916175213851,"mean":0.0011068817204301074,"variance":7.382102356190141e-9,"numSamples":62},"times":{"cycle":0.08301612903225805,"elapsed":5.72,"period":0.0011068817204301074,"timeStamp":1703252729142}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36":{"aborted":false,"count":79,"cycles":3,"hz":1004.8128543004093,"stats":{"moe":0.000011348602464623988,"rme":1.140322163479949,"sem":0.0000057901032982775445,"deviation":0.00004703902156201612,"mean":0.0009952101983172177,"variance":2.2126695495118173e-9,"numSamples":66},"times":{"cycle":0.0786216056670602,"elapsed":5.897,"period":0.0009952101983172177,"timeStamp":1703252759806}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment