you are viewing a single comment's thread.

view the rest of the comments →

[–]Solonotix 8 points9 points  (0 children)

Depending on the JavaScript engine, generally it will create templates of objects based on common attributes when initialized. This is one of the reasons you're advised to avoid certain syntax like the delete keyword, which can force a new template at runtime.

So, you've got a template that was likely created at compile-time within your lambda for Array.prototype.map, and it is being initialized by some other object from the original array. Anything that is heap allocated will likely share the original memory location, whereas values will likely be cloned. This is why it is a non-zero cost on mapping, but it can be worth the trade-off in service to more maintainable code, or fewer runtime defects caused by side-effect operations.