{"ScriptPreparationCode":"var deepCopyObject = (inObject) =\u003E {\r\n if (inObject instanceof Date) return inObject\r\n\r\n if (inObject instanceof Set) return new Set(inObject)\r\n\r\n if (inObject instanceof Map) return new Map(inObject)\r\n\r\n if (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\n return inObject // Return the value if inObject is not an object\r\n }\r\n\r\n // Create an array or object to hold the values\r\n const outObject = Array.isArray(inObject) ? [] : {}\r\n\r\n Object.keys(inObject).forEach((key) =\u003E {\r\n const value = inObject[key]\r\n\r\n // Recursively (deep) copy for nested objects, including arrays\r\n outObject[key] = typeof value === \u0027object\u0027 \u0026\u0026 value !== null ? deepCopyObject(value) : value\r\n })\r\n\r\n return outObject\r\n}\r\n\r\nfunction deepCopyObjectSimple(inObject) {\r\n if (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\n return inObject;\r\n }\r\n\r\n const outObject = Array.isArray(inObject) ? [] : {};\r\n\r\n for (const key in inObject) {\r\n const value = inObject[key];\r\n outObject[key] = (typeof value === \u0027object\u0027 \u0026\u0026 value !== null) ? deepCopyObjectSimple(value) : value;\r\n }\r\n\r\n return outObject;\r\n}\r\n\r\nvar isArray = Array.isArray;\r\n\r\nfunction copy(obj) {\r\n if (!obj) return obj;\r\n if (isArray(obj)) {\r\n var arr = [];\r\n var length = obj.length;\r\n for (var i = 0; i \u003C length; i\u002B\u002B) arr.push(copy(obj[i]))\r\n return arr;\r\n } else if (typeof obj === \u0027object\u0027) {\r\n var keys = Object.keys(obj);\r\n var length = keys.length;\r\n var newObject = {};\r\n for (var i = 0; i \u003C length; i\u002B\u002B) {\r\n var key = keys[i];\r\n newObject[key] = copy(obj[key]);\r\n }\r\n return newObject;\r\n }\r\n return obj;\r\n}\r\n\r\nvar objectKeys = Object.keys;\r\n\r\nfunction copyv2(val) {\r\n if (!val) return val;\r\n if (isArray(val)) {\r\n var arr = [];\r\n var length = val.length;\r\n for (var i = 0; i \u003C length; i\u002B\u002B) arr.push(copy(val[i]))\r\n return arr;\r\n } else if (typeof val === \u0027object\u0027) {\r\n var keys = objectKeys(val);\r\n var newObject = {};\r\n for (var i = keys.length - 1; i \u003E -1; i--) {\r\n var key = keys[i];\r\n newObject[key] = copy(val[key]);\r\n }\r\n return newObject;\r\n }\r\n return val;\r\n}\r\n\r\n\r\nfunction cloneRegExp(re) {\r\n const regexMatch = /^\\/(.*)\\/([gimyu]*)$/.exec(re.toString());\r\n return new RegExp(regexMatch[1], regexMatch[2]);\r\n}\r\n\r\nfunction clone(arg) {\r\n if (typeof arg !== \u0027object\u0027) {\r\n return arg;\r\n }\r\n if (arg === null) {\r\n return null;\r\n }\r\n if (Array.isArray(arg)) {\r\n return arg.map(clone);\r\n }\r\n const cloned = {};\r\n for (const name in arg) {\r\n if (Object.prototype.hasOwnProperty.call(arg, name)) {\r\n (cloned)[name] = clone(\r\n (arg)[name],\r\n );\r\n }\r\n }\r\n return cloned;\r\n}\r\n\r\n\r\nfunction deepCopyObjectIterative(inObject) {\r\n if (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\n return inObject;\r\n }\r\n\r\n const stack = [{ source: inObject, target: Array.isArray(inObject) ? [] : {} }];\r\n const root = stack[0].target;\r\n\r\n while (stack.length \u003E 0) {\r\n const { source, target } = stack.pop();\r\n\r\n for (const key in source) {\r\n const value = source[key];\r\n if (typeof value === \u0027object\u0027 \u0026\u0026 value !== null) {\r\n target[key] = Array.isArray(value) ? [] : {};\r\n stack.push({ source: value, target: target[key] });\r\n } else {\r\n target[key] = value;\r\n }\r\n }\r\n }\r\n\r\n return root;\r\n}\r\n\r\nvar MyObject = {\r\n description: \u0027Creates a deep copy of source, which should be an object or an array.\u0027,\r\n myNumber: 123456789,\r\n myBoolean: true,\r\n jayson: {\r\n stringify: \u0027JSON.stringify() method converts a JavaScript value to a JSON string....\u0027,\r\n parse: \u0027JSON.parse() method parses a JSON string...\u0027\r\n }\r\n};\r\n \r\nfunction generateNestedObject(depth, breadth) {\r\n if (depth === 0) {\r\n return \u0027end\u0027; // Base value at the deepest level\r\n }\r\n\r\n let result = {};\r\n for (let i = 0; i \u003C breadth; i\u002B\u002B) {\r\n result[\u0060key${i}\u0060] = {\r\n value: \u0060value${depth}-${i}\u0060,\r\n nestedArray: Array.from({ length: breadth }, (_, k) =\u003E \u0060item${depth}-${i}-${k}\u0060),\r\n nestedObject: generateNestedObject(depth - 1, breadth)\r\n };\r\n }\r\n return result;\r\n}\r\n\r\nvar MyHugeObject = generateNestedObject(7, 5);\r\n\r\nvar myCopy = null;","TestCases":[{"Name":"JSON.stringify","Code":"myCopy = JSON.parse(JSON.stringify(MyObject));","IsDeferred":false},{"Name":"structuredClone","Code":"myCopy = structuredClone(MyObject);","IsDeferred":false},{"Name":"deepCopyObject","Code":"myCopy = deepCopyObject(MyObject);","IsDeferred":false},{"Name":"deepCopyObjectSimple","Code":"myCopy = deepCopyObjectSimple(MyObject);","IsDeferred":false},{"Name":"deepCopyObjectIterative","Code":"myCopy = deepCopyObjectIterative(MyObject);","IsDeferred":false},{"Name":"copy","Code":"myCopy = copy(MyObject);","IsDeferred":false},{"Name":"clone","Code":"myCopy = clone(MyObject);","IsDeferred":false},{"Name":"copyv2","Code":"myCopy = copyv2(MyObject);","IsDeferred":false},{"Name":"JSON.stringify - huge","Code":"myCopy = JSON.parse(JSON.stringify(MyHugeObject));","IsDeferred":false},{"Name":"structuredClone - huge","Code":"myCopy = structuredClone(MyHugeObject);","IsDeferred":false},{"Name":"deepCopyObject - huge","Code":"myCopy = deepCopyObject(MyHugeObject);","IsDeferred":false},{"Name":"deepCopyObjectSimple - huge","Code":"myCopy = deepCopyObjectSimple(MyHugeObject);","IsDeferred":false},{"Name":"deepCopyObjectIterative - huge","Code":"myCopy = deepCopyObjectIterative(MyHugeObject);","IsDeferred":false},{"Name":"copy - huge","Code":"myCopy = copy(MyHugeObject);","IsDeferred":false},{"Name":"clone - huge","Code":"myCopy = clone(MyHugeObject);","IsDeferred":false},{"Name":"copyv2 - huge","Code":"myCopy = copyv2(MyHugeObject);","IsDeferred":false}]}