DEV Community

Discussion on: JS: use spread to exclude properties

Collapse
 
mschinis profile image
Michael Schinis

Although the gist of the above is correct, the right hand side of the following code creates a copy of the firstObject before extracting age, and creating secondObject, allocating more memory unnecessarily.

const {age, ...secondObject} = {...firstObject};
Enter fullscreen mode Exit fullscreen mode

The following achieves the same outcome, but skips the extra allocation.

const {age, ...secondObject} = firstObject
Enter fullscreen mode Exit fullscreen mode
Collapse
 
darksmile92 profile image
Robin Kretzschmar

Thank you for the explanation, you are absolutely correct about this. That was is more efficient 👍

Collapse
 
marcselman profile image
Marc Selman

You might wanna update your original post for people who don't read the comments. 😉

Thread Thread
 
darksmile92 profile image
Robin Kretzschmar

Good catch, thanks!
Lost track of it, updated :)