Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneJson Perf (small json)
(version: 3)
Comparing performance of:
lodash.cloneDeep vs cleanJson [modified to clone] (Recursive) vs customDeepClone (Recursive) vs cloneJson (Loop)
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var obj = { officeIpAddresses: [], customDomainHasUpdatedDns: false, stylePermissions: { styleAccess: { block: true, text: true, css: true, toolbar: true, motion: true, }, editStyle: false, }, permissions: { userInvitePermission: "admin", defaultProjectAccess: "none", }, notifications: { subscriptions: [], projectFirstOpen: true, }, modules: { quote: { savedLineItems: [{ type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [{ type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "TMS and ESS Licences", style: {}, }, ], }, quantity: 1, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Employee Licence", rate: 4.05, }, isTaxExempt: false, interactive: { isOptional: false, isOptionalSelected: false, isQuantityOptional: false, quantityRange: { min: 0, max: 0, }, isOptionalQuantity: false, }, currency: "GBP", id: "k55w3lGzGM4", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [{ type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "MF901 Stainless Steel T&A/Muster Point - PoE - (Beta Test Sites Only) (Reader Required - To be Identified from External Readers)", style: {}, }, ], }, quantity: 1, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1750, }, isTaxExempt: false, interactive: { isOptional: false, isOptionalSelected: false, isQuantityOptional: false, quantityRange: { min: 0, max: 0, }, isOptionalQuantity: false, }, currency: "GBP", id: "AOqDHpIIeAI", }, ], }, }, };
Tests:
lodash.cloneDeep
_.cloneDeep(obj);
cleanJson [modified to clone] (Recursive)
function cleanJson(obj) { // LOOP THROUGH ARRAYS let cleaned; if (obj instanceof Array) { cleaned = []; for (const elem of obj) { cleaned.push(cleanJson(elem)); } return cleaned; // CLEAN AN OBJECT } else if (obj && typeof obj === "object") { cleaned = {}; for (const key of Object.keys(obj)) { // If key starts with "$" it is a private variable // If the key starts with _ we are expecting it to be // _id, __v or __t, all of which we don't allow in the clean // JSON as it is not something we want affecting Mongo // switch (key.charAt(0)) { // case "$": // case "_": // continue; // } // If the key is not one of those on our persistance // blacklist, we clean the sub-JSON cleaned[key] = cleanJson(obj[key]); } return cleaned; } else { return obj; } } cleanJson(obj);
customDeepClone (Recursive)
function deepClone(obj) { let out; if (Array.isArray(obj)) { out = []; for (let index = 0; index < obj.length; ++index) { const subArray = obj[index]; out.push(typeof subArray === "object" ? deepClone(subArray) : subArray); } } else { out = {}; for (const key in obj) { const subObject = obj[key]; out[key] = typeof subObject === "object" ? deepClone(subObject) : subObject; } } return out; } deepClone(obj);
cloneJson (Loop)
function cloneJsonLoop(value) { if (!Array.isArray(value) && typeof value !== "object") return value; let copyObj; const vs = []; if (Array.isArray(value)) { copyObj = [...value]; } else if (typeof value === "object") { copyObj = { ...value }; } vs.push(copyObj); while (vs.length != 0) { const obj = vs.pop(); if (obj instanceof Array) { for (let i = 0; i < obj.length; i++) { const value = obj[i]; if (value instanceof Array) { obj[i] = value.slice(); vs.push(obj[i]); } else if (typeof value === "object") { obj[i] = Object.assign({}, value); vs.push(obj[i]); } } } /** typeof obj === object */ else { for (const key in obj) { const value = obj[key]; if (value instanceof Array) { obj[key] = value.slice(); vs.push(obj[key]); } else if (typeof value === "object") { obj[key] = Object.assign({}, value); vs.push(obj[key]); } } } } return copyObj; } cloneJsonLoop(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash.cloneDeep
cleanJson [modified to clone] (Recursive)
customDeepClone (Recursive)
cloneJson (Loop)
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):
Based on the provided benchmark results, it appears that the `customDeepClone (Recursive)` test is currently the fastest, with an average execution rate of 100442.6640625 executions per second. The `cloneJson (Loop)` test comes in second, with an average execution rate of 40355.57421875 executions per second. The `cleanJson [modified to clone] (Recursive)` test also runs well, with an average execution rate of 39894.58203125 executions per second. Finally, the `lodash.cloneDeep` method is slower, with an average execution rate of 34800.83203125 executions per second. It's worth noting that these results are specific to a Safari 13 browser on a Mac OS X 10.15.5 desktop platform, and may not generalize to other browsers or platforms. As for the "Test Name" column, it appears that the tests are being run with varying levels of modification to their original implementations. The `customDeepClone (Recursive)` test seems to be the most straightforward implementation, while the `cloneJson (Loop)` test has been modified to use a loop-based approach. The `cleanJson [modified to clone] (Recursive)` test also appears to have some modifications, but its exact nature is unclear from the provided data. Without more context or information about the specific use cases and requirements for each test, it's difficult to provide a definitive answer on which implementation is "best." However, based on the benchmark results alone, it seems that the `customDeepClone (Recursive)` test is currently the most efficient.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs Pvorb Clone - Fixed
Lodash cloneDeep 4.17.10 vs JSON Clone
Lodash cloneDeep vs JSON Clone 2323123132123
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs Pvorb Clonesdsd
Comments
Confirm delete:
Do you really want to delete benchmark?