Fastest way to flatten / un-flatten nested JavaScript objects
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Flattening a nested JavaScript object means converting deep structure into key paths such as user.address.city. Un-flattening reverses the process. The fastest practical approach is usually a single traversal that writes into a result object, but performance only matters if the transformation is also reversible and unambiguous.
Decide on a Path Convention First
Before writing code, choose how paths are represented. Common options are:
- dot notation such as
user.name - bracket notation such as
items[0].id - slash notation such as
user/address/city
The convention matters because flattening and un-flattening must agree on how arrays, numbers, and keys containing dots are handled.
If you skip that design step, the “fast” function becomes useless on real data.
A Recursive Flatten Function
For ordinary objects and arrays, a recursive traversal is clear and fast enough for most applications.
This visits each property once, so the time cost is roughly proportional to the number of nodes in the structure.
Rebuild the Nested Object
Un-flattening walks each path and creates the missing containers.
This produces nested objects again, including arrays when the next path segment looks numeric.
What “Fastest” Usually Means
For normal application code, the main performance wins come from:
- one pass over the structure
- avoiding repeated deep copies
- avoiding expensive regex or string rebuilding inside tight loops when unnecessary
- not serializing to JSON just to reshape keys
The complexity is still mostly linear in the number of values processed. For huge objects, recursion depth can become the real bottleneck rather than raw traversal speed.
If you expect extremely deep input, an explicit stack-based iterative implementation may be safer than recursion.
Reversibility Is Harder Than Flattening
Flattening is easy when you only need a display or logging format. It becomes harder when you need exact reversibility.
Examples of ambiguity include:
- original keys that already contain dots
- deciding whether
a.0means array index0or object key"0" - preserving special values such as
Date,Map, or custom class instances
A robust flattening scheme must define those rules up front.
Common Pitfalls
- Treating path format as an afterthought instead of a design choice.
- Ignoring arrays and then discovering un-flattening cannot rebuild the original shape.
- Using a reversible-looking format that breaks when keys contain dots.
- Optimizing microseconds before confirming the transformation rules are correct.
- Recursing into extremely deep structures without considering call-stack limits.
Summary
- The practical fast approach is a single traversal that writes flattened paths into one result object.
- Un-flattening must follow the same path convention or the transformation is not reversible.
- Arrays, numeric keys, and dotted keys need explicit rules.
- For most cases, recursion is clear and fast enough.
- Correctness of the path scheme matters more than chasing a tiny performance gain.
Related reading
- Fastest way to sort 32bit signed integer arrays in JavaScript?
- Fetch request failing in Node.js Client network socket disconnected before secure TLS connection was established
- File Upload in WebView
- Find all permutations of 2 arrays in JS
- Find an object in array?
- Find an object in array?
- Find difference between two strings in JavaScript
- Find the min/max element of an array in JavaScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.