I've been experimenting with concepts of immutability with javascript objects. I was wondering if the following code example implements what I believe is known as "Structural Sharing" (see: https://www.youtube.com/watch?v=e-5obm1G_FY&start=1123).
const objectFirst = {
key1: 1,
key2: 2
}
const updateObject = (lastObject) => {
const updatedObject = {...lastObject, ...{ key2: 4 }} // Object.assign({}, lastObject, { key2: 4 })
return updatedObject
}
const objectNext = updateObject(objectFirst)
objectNext is now { key1: 1, Key2: 4 } and objectFirst is unchanged. But has key1 been duplicated? Or, is their now essentially a reference to key1's location in memory shared by both objects?
I'm basically just asking if this approach implements some sort of "Structural Sharing"? One could see that if this were not the case, then it would lead to significant memory bloat.
objectFirst.key1and see if it reflects inobjectNext.key1properties. There's no property sharing in any visible way, though one never knows what goes on internally.