Getting my head around shallow vs deep copy

Last night while attempting to pseudocode my way into finding adjacent nodes (either dynamically _or _manually at this point. Later Graphikos from the Impact forums/IRC was kind enough to show me a modified function he had and explain how it works, which may be what I need with some tweaking - I’ll write more on this later) I found myself pouring through Impact documentation in hopes that maybe I’ll find something that might help. While doing so I stumbled across this:

[caption id=“attachment_47409” align=“aligncenter” width=“624” caption=“”] Deep copy of object in ImpactJS [/caption]

I never knew this existed. The thing is, I’m currently using slice() to copy my global which is stored in the Controller into the moving entity when it’s spawned. I described this briefly in my previous post.

From what I’ve been reading (because this made me read more about shallow copy vs deep copy), slice() creates a shallow copy of the array - so from what I understand and this could be completely wrong, right now my copy of the array is still pointing to the same objects as the global array. If those entities change, so will the properties of my path…

And from what I gather a deep copy duplicates not just the references to objects in the array but the objects themselves. So if something inside one or more of my node entities changes while the moving entities are moving from node to node, it will not impact their versions of the nodes. Do I even have the right idea here?

I’ve tested replacing the code I have now in my moving entities code:

[crayon]this.path_array_copy =;[/crayon]

with this to create a deep copy:

[crayon]this.path_array_copy = ig.copy( );[/crayon]

Visually when I run this it looks exactly the same and it seems like if I’m understanding deep vs shallow copy correctly, deep copy would be more resource intensive/slow because it’s actually making copies of objects as opposed to making copies of references to objects. So I reverted back to the slice() method of doing this for now, but left the deep copy code commented out just below it - just in case I later find that for some reason something that happens to any of those nodes while entities are en route screws up their behaviour somehow as I add different features and functionality.

I should stop distracting myself with deep vs shallow copy now and get back onto that adjacency stuff. But I also need to do more reading later to confirm whether I’m even understanding shallow and deep copy correctly.

© - 2021 · Liza Shulyayeva ·