Accepted answer

let's consider the example below

clone and clone2 are shallow, only properties of original object are affected. clone3 and clone4 are deep.

however if the spread operator only creates deep copy sometimes, then how can i test if the new object is a deep copy or not?

it creates deep copy in case of clone4 - as long as the depth is controlled by a developer. generally there's no need to test if an object is deep copy or just different in react, because this check is expensive and requires to traverse nested objects in both compared objects and compare them property by property.

performance is the reason why react relies on immutability. if a new value isn't === equal, it's considered a different value.

so james is a shallow copy in facebook's code.

it isn't. it's a reference that was assigned to another variable. it's still same object.

are reference and shallow copy exactly the same thing in js?

a reference isn't a copy. so it isn't shallow copy, too.

i commented nbachampion out in clone4, so now nbachampion is a reference rather than copy! if i push a new value in user.highlights.nbachampion, clone4 will also updates. what should we call this type of object? it's neither a shallow nor deep copy.

it's just a copy. it doesn't have specific name, because there's rarely a need to do such selective copies. if the intention is to make it act like deep copy, it should be called a mistake.


a shallow copy of an object (or array) is a separate object with a matching set of property names and property values.

after making a shallow copy, a comparison on a property-by-property basis of the two objects (the original and the copy) will show all property values being ===.

for example:

let o1 = { a: 1, b: 2, c: { x: "hello", y: "world" } };
let o2 = {};
object.keys(o1).foreach(propertyname => o2[propertyname] = o1[propertyname]);

now if the property values of o1 and o2 are compared, they are of course ===. in particular, property "c" of both objects will be a reference to that sub-object with "x" and "y" property names. however, comparing o1 and o2 with either == or === will not show equality, because two distinct objects are never == to each other regardless of their contents.

a deep copy of an object is one where every object-valued property of the source is recursively deep-copied into the destination copy. because new object values are of necessity created for the deep-copy target, those property values will not compare as ===, because no two distinct objects can be ===.

making deep copies in a language like javascript can be problematic because the "graph" of references from property values can be circular, and because some property values may be functions. generally a deep copy method has to make some assumptions on an application by application basis.

in my experience, the need to make a deep copy of an object is quite rare compared to making shallow copies.

now, all that aside, comparison between two object references as in your bit of react code:

shouldcomponentupdate(nextprops) {
  return this.props.children !== nextprops.children;

has nothing to do with shallow or deep copies. that's a single comparison between two object properties, both named "children". the objects referenced by this.props and nextprops may be different objects or they may be the same object, and one may be a shallow or deep copy of the other, but that makes no difference for that comparison statement: all that's doing is comparing the two particular "children" property values for strict inequality. (it's true that if nextprops happens to be a shallow copy of this.props, or vice-versa, that !== comparison would be false, but the comparison doesn't have to know about the prior history of the two objects; it's just a single comparison of two values.)

Related Query

More Query from same tag