2

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.

3
  • 3
    No this is not that. You can verify that by changing objectFirst.key1 and see if it reflects in objectNext. Commented Oct 4, 2017 at 14:47
  • Both objects have distinct key1 properties. There's no property sharing in any visible way, though one never knows what goes on internally. Commented Oct 4, 2017 at 14:48
  • For structural sharing you need a tree-like data structure. The simplest tree is an unary tree, where each node has one or no child. You guessed it, it is a single linked list I am talking about. When you implement such a list you will notice that prepending is a cheap operation, whereas appending is an expensive one. Therefore balanced trees are used for the latter. Commented Oct 5, 2017 at 6:51

2 Answers 2

1

No, because 1 is stored as a simple value on objectFirst, not a reference to an object, so when you do {...objectFirst} you're copying the value of objectFirst.key1 to objectNext.key1.

However, if objectFirst.key1 was an object instead, then the shallow copy would copy the object reference instead of creating a new object. As a result, objectFirst.key1 and objectNext.key2 would both reference the same object.

const objectFirst = {
    key1: 1,
    key2: 2,
    sharedObject: {foo: 'original string'} 
}
// we make a shallow copy with a different key2 value.
const objectSecond = {...objectFirst, key2: 4};

// changing a value in objectSecond doesn't affect objectFirst
objectSecond.key1 = 100;
console.log(objectFirst.key1); // 1

// but sharedObject is shared
objectSecond.sharedObject.foo = "new string";
console.log(objectFirst.sharedObject); // {foo: "new string"}

Sign up to request clarification or add additional context in comments.

Comments

0
console.log(objectFirst['key1'] === objectNext['key1']); // true 

console.log(objectFirst['key2'] === objectNext['key2']); // false

This means objectFirst and objectNext share the key1 property.

1 Comment

I don't think so, because key1 is not reference value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.