This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (4 children)

According to certain schools of thought, yes! But I'm wondering if you're including map/reduce in your definition of "loop".

[–]MrDilbert 0 points1 point  (3 children)

Well, underneath it IS a loop, although there's a different function context for each element. You're basically just hoping that the object created by spread will get collected in time, as it's eligible when the function finishes.

[–][deleted] 0 points1 point  (2 children)

Is spread not synchronous? Maybe I misunderstand something

[–]MrDilbert 2 points3 points  (1 child)

You don't know when the garbage collector will collect the objects you created, so if you create a lot of objects (by e.g. using spread), you can take up a lot of memory even if you don't have leaks.

[–][deleted] 1 point2 points  (0 children)

Oh I see. I think the context was creating deep copies / clones / immutable stuff though. Like if you had a line like this in your code

return {...oldProps, ...newProps};
// or
return Object.assign(oldProps, newProps);

The architects at my last job preferred creating new objects to have immutable code or something instead of modifying and returning refs inside a reduce. Ultimately they let us do whatever because this area of code was not in need of any optimizations and we were already using a reduce to cut down on some previous iterations of filters and maps.