Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Copy the Budget All the Way Down

In this video, you gave Dana a what-if budget that’s separate from her real one, all the way down, by copying it with structuredClone().

Key Points

  • Every file that imports budget shares one object, so a what-if budget needs a copy. Dana tries copies out in a short file, whatif.js, next to her tracker.
  • Spread copies each top-level property into a new object. The value of limits is a reference to an object, so { ...budget } gets the same reference, and both budgets share one limits object. The same goes for startsOn: both budgets share one Date. Cutting groceries on the copy changed the real budget in that run too, and each run starts again from the 450 and 150 in budget.js. That’s a shallow copy.
  • Object.assign({}, budget) copies the properties of budget into the empty object you give it, and returns that same object. It’s shallow too. Watch out: a new object on the outside doesn’t mean new objects inside.
  • A deep copy copies every level. JSON.parse(JSON.stringify(budget)) builds brand-new objects from the text, so the real limits stayed at 450 and 150. However, JSON turned startsOn into a string, and JSON.parse() has no way to know it was ever a date.
  • Watch out: a JSON round trip is a safe copy only for data that’s already JSON-shaped, since it also drops undefined properties and turns a Map into an empty object.
  • structuredClone() is a global function that Node and browsers provide. It makes a deep copy and keeps some types JSON can’t hold, such as Dates and Maps. The copy’s startsOn is no longer a string, and it’s a Date of its own. It can’t copy functions, though, and throws an error instead. Dana’s budget is plain data, so that’s no problem here.
  • When the copy must be separate all the way down, structuredClone() is the tool to use.
Copy tool What it copies
{ ...budget }, Object.assign() The top level only
JSON.parse(JSON.stringify()) Every level; a Date becomes a string, a Map becomes {}, undefined properties are dropped
structuredClone() Every level; keeps Dates and Maps; throws an error on functions

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Dana wants to try a what-if budget. What if she spent less on groceries and more on fun? She’d cut groceries to 400 and raise fun to 200, without touching her real budget.

00:12 Her real budget lives in budget.js, as one exported object. It’s nested, because the limits are an object inside the budget object. Its start date is a real Date object, not text.

00:26 Every file that imports budget shares that one object, so changing it changes the real budget. The what-if needs a copy. To try copies out, Dana writes a short file, whatif.js, next to her tracker.

00:42 The first copy uses spread, which you already know. whatif.js spreads budget into a new object, then cuts groceries and raises fun on the copy. Its last line logs the real limits.

00:56 Run it, and the real budget in this run changed too. Groceries are 400, and fun is 200.

01:04 Spread copies each top-level property into the new object. The value of limits is a reference to an object, so the copy gets the same reference. Both budgets share one limits object, and one start date too.

01:17 That’s a shallow copy. Object.assign is a second copy tool. It copies the properties of budget into the empty object you give it, and returns that same object.

01:28 Swap it in and run the file, which starts from 450 again, and the real limits change again. Watch out: a new object on the outside doesn’t mean new objects inside.

01:40 Neither tool copies past the first level.

01:45 You need a deep copy, one that copies every level. You already have one way to make it. JSON.stringify turns the budget into text, and JSON.parse builds brand-new objects from that text, at every level.

02:00 So the copy line becomes the round trip. Now whatif.js logs the real limits and the what-if limits, and then the type of the copy’s start date. Your turn: pause the video and predict what the last line prints.

02:20 It prints string, not object. The limits worked. The real budget still says 450 and 150, and the what-if has its own. But JSON turned the Date into a string, and JSON.parse has no way to know it was ever a date.

02:38 Watch out: a JSON round trip is a safe copy only for data that’s already JSON-shaped, since it also drops undefined properties and turns a Map into an empty object.

02:51 Node and browsers both give you structuredClone, a global function built for this job. It makes a deep copy, and it keeps types that JSON can’t hold. Swap it into the copy line and run whatif.js again.

03:05 The real limits stay at 450 and 150, and the what-if has 400 and 200.

03:13 The last line prints object this time, so the start date isn’t a string anymore. In the drawing, each budget now has its own Date. It copies a Map as well.

03:25 Dana’s budget has no Map, so here’s a quick look in the REPL. structuredClone gives back a Map with the same entry. The JSON round trip gives back an empty object.

03:37 Spread and Object.assign copy one level. A JSON round trip copies every level, but it changes Dates and Maps, and drops undefined properties. structuredClone copies every level, and keeps Dates and Maps, but it can’t copy functions and throws an error instead.

03:55 As a rule, when the copy must be separate all the way down, structuredClone is the tool to use.

04:03 Dana now has a what-if budget that leaves the real one alone. Next, in Compare Budgets Value by Value, you’ll check whether the what-if really differs from her real budget.

Become a Member to join the conversation.