My Personal Experience Managing Local Changes in Database with `@ungap/structured-clone`


So I was recently working on a web app and hit a classic developer headache:
“How do I make changes to a local copy of my database data without messing up the original?”
Whether you’re working with forms, dashboards, or temporary updates — this problem always sneaks in.
That’s when I discovered a small but powerful package:
👉 @ungap/structured-clone
It instantly made my workflow cleaner, safer, and a lot less buggy. Here's how.
The Problem: Local Changes, Global Mess
Here was my scenario:
I fetched a complex object from MongoDB.
I wanted to update some values locally — maybe a field like
status
ortitle
.But I didn’t want to mutate the original data until the changes were confirmed or submitted.
Spread Operator or Object.assign()
? Not Enough.
I tried the usual tricks:
const copy = { ...originalData };
// or
const copy = Object.assign({}, originalData);
These only perform shallow copies — they don’t help if your object has nested fields, arrays, or objects inside.
One small update in the nested data, and 💥 — the original object gets affected too.
What Is @ungap/structured-clone
?
It’s a tiny NPM package — a polyfill for the modern structuredClone()
Web API.
(Which isn't available in all Node.js versions yet.)
It supports deep cloning of:
Objects
Arrays
Maps & Sets
Dates
Nested values (yep, even deeply nested structures)
Basically, it’s like hitting “Duplicate Everything Safely” for any JavaScript value.
🔧 How I Used It
Installation
npm install @ungap/structured-clone
Usage in My Project
import structuredClone from '@ungap/structured-clone';
const originalData = await getDataFromDatabase();
const localCopy = structuredClone(originalData);
// Make safe local changes
localCopy.status = "Published";
localCopy.updatedAt = new Date();
// Save only after confirming
await saveToDatabase(localCopy);
Simple. Safe. And it just works.
Why I Loved It
Here are the main benefits I experienced:
No accidental mutations
Peace of mind while editing deeply nested structures
Great for form editing, draft saving, or state management
When Should You Use This?
If you're building apps with:
Form editing with temporary state
Undo/redo functionality
Draft and review workflows
Any app where users change data before saving
This tiny tool can save you from unexpected bugs and data inconsistencies.
Bonus: It Plays Well with React
If you’re into the React or MERN stack world like me, using structured cloning for local state updates (especially in context or reducers) can be a life-saver.
Final Thoughts
Sometimes it's the smallest packages that solve the biggest frustrations.
@ungap/structured-clone
gave me the confidence to work with data safely, even in the most complex workflows.
If you're still manually deep-cloning with weird hacks or libraries, give this a shot — I promise you won't regret it.
NPM Link
Have you used structuredClone before? Or faced any horror stories with accidental data mutations?
Let’s chat in the comments 👇
Subscribe to my newsletter
Read articles from Muhammed Safwan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
