Skip to content

Instantly share code, notes, and snippets.

@gquittet
Last active October 23, 2019 16:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gquittet/67b5c62f411c3f01ce06c4b976169b5a to your computer and use it in GitHub Desktop.
Save gquittet/67b5c62f411c3f01ce06c4b976169b5a to your computer and use it in GitHub Desktop.
JavaScript deep copy
function deepCopy(o) {
// Cas de base
if (typeof o !== 'object' || o === null || o instanceof RegExp || o instanceof Date) {
return o
}
// Array
if (Array.isArray(o) || o instanceof Int8Array || o instanceof Uint8Array || o instanceof Uint8ClampedArray || o instanceof Int16Array || o instanceof Uint16Array || o instanceof Int32Array || o instanceof Uint32Array || o instanceof Float32Array || o instanceof Float64Array || o instanceof BigInt64Array || o instanceof BigUint64Array) {
return o.map(e => deepCopy(e))
}
// Object
const finalObject = {}
for (let k in o) {
if (o.hasOwnProperty(k)) {
finalObject[k] = deepCopy(o[k])
}
}
return finalObject
}
function deepCopy(o) {
// Cas de base
if (typeof o !== 'object' || o === null || o instanceof RegExp || o instanceof Date) {
return o
}
// Array
if (Array.isArray(o) || o instanceof Int8Array || o instanceof Uint8Array || o instanceof Uint8ClampedArray || o instanceof Int16Array || o instanceof Uint16Array || o instanceof Int32Array || o instanceof Uint32Array || o instanceof Float32Array || o instanceof Float64Array || o instanceof BigInt64Array || o instanceof BigUint64Array) {
return o.map(e => deepCopy(e))
}
// Object
const finalObject = {}
for (let k in o) {
if (o.hasOwnProperty(k)) {
finalObject[k] = deepCopy(o[k])
}
}
return finalObject
}
function testDeepCopyObject() {
console.log('Bad { ...a }:')
console.log('-------------')
a = { toto: { tata: () => 'Hey Man!' }, titi: 15, e: { f: /\w/ }, g: { h: new Int32Array([1, 2, 3]) }, i: { j: new Date('1970-01-01') } }
b = { ...a }
console.log('a', a)
console.log('b', b)
console.log('')
a.toto.tata = 'fkdfjkdfjkdfj'
a.g.h[2] = 1000
a.i.j = new Date('2010-12-30')
console.log('a', a)
console.log('b', b)
console.log('')
console.log('Bad JSON.parse(JSON.stringify(a)):')
console.log('----------------------------------')
a = { toto: { tata: () => 'Hey Man!' }, titi: 15, e: { f: /\w/ }, g: { h: new Int32Array([1, 2, 3]) }, i: { j: new Date('1970-01-01') } }
b = JSON.parse(JSON.stringify(a))
console.log('a', a)
console.log('b', b)
console.log('')
a.toto.tata = 'fkdfjkdfjkdfj'
a.g.h[2] = 1000
a.i.j = new Date('2010-12-30')
console.log('a', a)
console.log('b', b)
console.log('')
console.log('Good deepCopy:')
console.log('--------------')
a = { toto: { tata: () => 'Hey Man!' }, titi: 15, e: { f: /\w/ }, g: { h: new Int32Array([1, 2, 3]) }, i: { j: new Date('1970-01-01') } }
b = deepCopy(a)
console.log('a', a)
console.log('b', b)
console.log('')
a.toto.tata = 'fkdfjkdfjkdfj'
a.g.h[2] = 1000
a.i.j = new Date('2010-12-30')
console.log('a', a)
console.log('b', b)
}
function testDeepCopyArray() {
console.log('Bad [...a]:')
console.log('-------------')
a = [[1, 2, 3], 4, 5, [[6, 7, 8], 9], 0]
b = [...a]
console.log('a', a)
console.log('b', b)
console.log('')
a[0][0] = 'ici'
a[3][0][1] = 'toto'
console.log('a', a)
console.log('b', b)
console.log('')
console.log('Bad JSON.parse(JSON.stringify(a)):')
console.log('----------------------------------')
a = [[1, 2, 3], 4, 5, [[6, 7, 8], 9], 0]
b = JSON.parse(JSON.stringify(a))
console.log('a', a)
console.log('b', b)
console.log('')
a[0][0] = 'ici'
a[3][0][1] = 'toto'
console.log('a', a)
console.log('b', b)
console.log('')
console.log('Good deepCopy:')
console.log('--------------')
a = [[1, 2, 3], 4, 5, [[6, 7, 8], 9], 0]
b = deepCopy(a)
console.log('a', a)
console.log('b', b)
console.log('')
a[0][0] = 'ici'
a[3][0][1] = 'toto'
console.log('a', a)
console.log('b', b)
}
testDeepCopyObject()
console.log('')
console.log('')
console.log('')
console.log('')
console.log('')
console.log('')
testDeepCopyArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment