Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneJson Perf
(version: 2)
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: "Biometric Clock", }, ], }, quantity: 5, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1500, }, isTaxExempt: false, interactive: { isOptional: true, isOptionalSelected: false, isQuantityOptional: true, quantityRange: { min: 0, max: 5, }, }, currency: "GBP", id: "iQWwrO6qLew", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [{ subType: "paragraph", type: "block", }, { content: "Proximity Clock", subType: "text", type: "inline", }, ], }, quantity: 5, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1500, }, isTaxExempt: false, interactive: { isOptional: true, isOptionalSelected: false, isQuantityOptional: true, quantityRange: { min: 0, max: 100, }, }, currency: "GBP", id: "u0eMdVcAx1U", }, { type: "text", description: { content: "<p><br></p>", widgets: {}, tokens: [{ type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "T&A Clocking Terminals", style: {}, }, ], }, interactive: { isOptional: false, isOptionalSelected: false, }, id: "dPVSGJMm97g", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [{ type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "MF900 Stainless Steel T&A/Muster Point (UK 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: "BCLCCimoZ6M", }, { 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 Object.keys(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):
A benchmarking challenge! After analyzing the raw data, I'll provide an answer based on the latest benchmark result. **Answer:** The Chrome 86 browser with Mac OS X 10.15.5 execution is the fastest at approximately **521922 executions per second**. This suggests that the `cloneJson (Loop)` test is the most efficient among the four options tested, likely due to its iterative approach and use of array methods like `slice()` and `Object.assign()`.
Related benchmarks:
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 big object 12345
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs Pvorb Clonesdsd
npm clone lib
Comments
Confirm delete:
Do you really want to delete benchmark?