justmoon / node-extend

Simple function to extend objects
MIT License
341 stars 68 forks source link

Curcular error with deep copy #41

Closed KROT47 closed 4 years ago

KROT47 commented 8 years ago

I was looking where program falls in recursion for hour)

Its just object had link to itself... Check yourself: a = {}; a.a = a; extend( true, {}, a )

Hope it will save some time for somebody.

ljharb commented 8 years ago

Certainly if you have a circular reference, you should expect an infinite loop.

Your check certainly stops one level of infinite recursion due to a circular reference, but it'd be impossible toe fully fix this problem without keeping an array/Set of all seen objects, and checking that every time - a very expensive operation.

I don't think this is worth fixing on a shallow level, without fixing the entire problem. Circular references should be avoided anyways.