swelet
swelet

Reputation: 8722

Object.assign(), functional approaches and memory usage

If I do this in Javascript (ES6):

let originalState = [
    { id : 1, name : "One"},
    { id : 2, name : "Two"},
    { id : 3, name : "Three"}
]

let newState = Object.assign({}, ...originalState)

Will the objects in "newState" reference the objects from "orignalState" in memory, or will they be cloned, taking up new memory?

Upvotes: 0

Views: 1067

Answers (1)

Bergi
Bergi

Reputation: 665020

Will the objects in "newState" reference the objects from "orignalState" in memory, or will they be cloned, taking up new memory?

There won't be multiple new objects. There is only one object that is stored in the newState variable, it's the one you've created with the object literal:

let newState = Object.assign({}, ...originalState)
//                           ^^ here

The properties in newState will be created by standard assignment - they will exist separately from the ones of the originalState objects but contain the same value.

Upvotes: 2

Related Questions