Efficient algorithm to get the combinations of all items in object
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When developers ask for "all combinations of items in an object," they usually mean the cartesian product of the arrays stored under each key. That is a very different problem from generating subsets of one list. The real difficulty is not writing the loop. It is handling explosive output growth without wasting memory or creating work you never use.
Define the Combination Problem Precisely
Suppose the input is an object whose values are arrays:
If you want one result for every choice of one value from each key, the total number of combinations is the product of the array lengths. In the example above, the output size is 2 * 2 * 2 = 8.
That is why the first performance question is always output size. If the input has ten keys with ten choices each, the answer is already too large to materialize casually.
A Straightforward Iterative Product
For moderate sizes, a reduce-based cartesian product is clear and efficient enough.
This works well when the total result size is manageable and the caller genuinely needs all rows in memory at once.
Use a Generator When the Output Is Large
If the result space is large, generating combinations lazily is a better design. A generator can emit one combination at a time so downstream code can stop early or stream results to another system.
This does not change the mathematical growth of the problem, but it drastically lowers peak memory usage. That is often the real win.
Prune Invalid Branches Early
In production code, not every theoretical combination is valid. Some choices may be mutually exclusive, or certain keys may depend on earlier selections. If you know those rules, apply them during generation rather than filtering after full expansion.
Early pruning is often more important than small syntax-level optimizations because it reduces the number of objects you ever create.
Estimate the Size Before You Generate
Before generating anything, compute the expected number of rows. That gives you a simple guardrail for deciding whether to materialize, stream, or reject the request. It also helps you explain the cost of the request to other engineers before the job lands in production.
If the estimate is already too large for memory or latency budgets, the right solution is not a faster loop. It is a different product decision, such as pagination, filtering, or a streaming pipeline.
Avoid Confusing Cartesian Products With Subset Generation
This problem is frequently mislabeled. Subset generation chooses some items from one list. Cartesian expansion chooses one value from each independent dimension. The algorithms are different because the output structures are different.
Being precise about the problem helps you choose the right vocabulary, the right tests, and the right performance expectations. It also prevents teams from importing a combinations library that solves the wrong problem elegantly and still fails at the real task.
Common Pitfalls
The biggest mistake is materializing the full cartesian product without first estimating how large it will be. Another is filtering invalid results only after generating all of them. Teams also confuse subset problems with cross-key products, or they optimize the loop body while ignoring the exponential output size that dominates the whole task. A final mistake is copying huge intermediate objects on every step when only a streamed consumer was needed.
Summary
- Most "all combinations from an object" problems are cartesian products.
- The output size grows as the product of the option counts, not linearly.
- Iterative materialization is fine for moderate result sizes.
- Generators are better when you need to stream or stop early.
- Estimate size and prune invalid branches before worrying about micro-optimizations.
Related reading
- Efficient Algorithm to obtain Points in a Circle around a Center
- Efficient algorithm to randomly select items with frequency
- Efficient Algorithms for Computing a matrix times its transpose
- Efficient Array Storage for Binary Tree
- Efficient Cartesian Product algorithm
- Efficient data structure for sparse data lookup
- Efficient AVX2 implementation of a 17x17-bit squaring operation with result truncation
- Efficient combinations of N colored elements with restriction in the number of colors

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.