{"ScriptPreparationCode":"let deepCopyObject = (inObject) =\u003E {\r\nif (inObject instanceof Date) return inObject\r\n\r\nif (inObject instanceof Set) return new Set(inObject)\r\n\r\nif (inObject instanceof Map) return new Map(inObject)\r\n\r\nif (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\nreturn 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\nconst outObject = Array.isArray(inObject) ? [] : {}\r\n\r\nObject.keys(inObject).forEach((key) =\u003E {\r\nconst 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\nreturn outObject\r\n}\r\n\r\nfunction deepCopyObjectSimple(inObject) {\r\nif (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\nreturn inObject;\r\n}\r\n\r\nconst outObject = Array.isArray(inObject) ? [] : {};\r\n\r\nfor (const key in inObject) {\r\nconst value = inObject[key];\r\noutObject[key] = (typeof value === \u0027object\u0027 \u0026\u0026 value !== null) ? deepCopyObjectSimple(value) : value;\r\n}\r\n\r\nreturn outObject;\r\n}\r\n\r\nlet isArray = Array.isArray;\r\n\r\nfunction copy(obj) {\r\nif (!obj) return obj;\r\nif (isArray(obj)) {\r\nlet arr = [];\r\nlet length = obj.length;\r\nfor (let i = 0; i \u003C length; i\u002B\u002B) arr.push(copy(obj[i]))\r\nreturn arr;\r\n} else if (typeof obj === \u0027object\u0027) {\r\nlet keys = Object.keys(obj);\r\nlet length = keys.length;\r\nlet newObject = {};\r\nfor (let i = 0; i \u003C length; i\u002B\u002B) {\r\nlet key = keys[i];\r\nnewObject[key] = copy(obj[key]);\r\n}\r\nreturn newObject;\r\n}\r\nreturn obj;\r\n}\r\n\r\nlet objectKeys = Object.keys;\r\n\r\nfunction copyv2(val) {\r\nif (!val) return val;\r\nif (isArray(val)) {\r\nlet arr = [];\r\nlet length = val.length;\r\nfor (let i = 0; i \u003C length; i\u002B\u002B) arr.push(copy(val[i]))\r\nreturn arr;\r\n} else if (typeof val === \u0027object\u0027) {\r\nlet keys = objectKeys(val);\r\nlet newObject = {};\r\nfor (let i = keys.length - 1; i \u003E -1; i--) {\r\nlet key = keys[i];\r\nnewObject[key] = copy(val[key]);\r\n}\r\nreturn newObject;\r\n}\r\nreturn val;\r\n}\r\n\r\n\r\nfunction cloneRegExp(re) {\r\nconst regexMatch = /^\\/(.*)\\/([gimyu]*)$/.exec(re.toString());\r\nreturn new RegExp(regexMatch[1], regexMatch[2]);\r\n}\r\n\r\nfunction clone(arg) {\r\nif (typeof arg !== \u0027object\u0027) {\r\nreturn arg;\r\n}\r\nif (arg === null) {\r\nreturn null;\r\n}\r\nif (Array.isArray(arg)) {\r\nreturn arg.map(clone);\r\n}\r\nconst cloned = {};\r\nfor (const name in arg) {\r\nif (Object.prototype.hasOwnProperty.call(arg, name)) {\r\n(cloned)[name] = clone(\r\n(arg)[name],\r\n);\r\n}\r\n}\r\nreturn cloned;\r\n}\r\n\r\n\r\nfunction deepCopyObjectIterative(inObject) {\r\nif (typeof inObject !== \u0027object\u0027 || inObject === null) {\r\nreturn inObject;\r\n}\r\n\r\nconst stack = [{ source: inObject, target: Array.isArray(inObject) ? [] : {} }];\r\nconst root = stack[0].target;\r\n\r\nwhile (stack.length \u003E 0) {\r\nconst { 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\nreturn root;\r\n}\r\n\r\nlet MyObject = {\r\ndescription: \u0027Creates a deep copy of source, which should be an object or an array.\u0027,\r\nmyNumber: 123456789,\r\nmyBoolean: true,\r\njayson: {\r\nstringify: \u0027JSON.stringify() method converts a JavaScript value to a JSON string....\u0027,\r\nparse: \u0027JSON.parse() method parses a JSON string...\u0027\r\n}\r\n};\r\n\r\nfunction generateNestedObject(depth, breadth) {\r\nif (depth === 0) {\r\nreturn \u0027end\u0027; // Base value at the deepest level\r\n}\r\n\r\nlet result = {};\r\nfor (let i = 0; i \u003C breadth; i\u002B\u002B) {\r\nresult[\u0060key${i}\u0060] = {\r\nvalue: \u0060value${depth}-${i}\u0060,\r\nnestedArray: Array.from({ length: breadth }, (_, k) =\u003E \u0060item${depth}-${i}-${k}\u0060),\r\nnestedObject: generateNestedObject(depth - 1, breadth)\r\n};\r\n}\r\nreturn result;\r\n}\r\n\r\nlet MyHugeObject = generateNestedObject(7, 5);\r\n\r\nlet 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}]}