DEV Community

Cover image for Finding the Balance: When to Use Object Destructuring and When to Avoid It
Andreas Bergström
Andreas Bergström

Posted on • Edited on • Originally published at andreasbergstrom.dev

Finding the Balance: When to Use Object Destructuring and When to Avoid It

Object destructuring on function parameters gives TypeScript the named arguments it doesn't have natively — function greet({ name, greeting = 'Hello' }: GreetingOptions) reads as self-documenting at the call site, defaults fall out for free, and you can add new optional fields without breaking existing callers. The cost is a small allocation per call.

The full post weighs the readability and extensibility wins against the cases where positional args still win — one or two mandatory args, simple callbacks, math/utility functions with a well-known order — and where the per-call overhead actually shows up: high-frequency trading, real-time pipelines, hot loops you've already profiled.


Originally published at andreasbergstrom.dev — read the full post there.

Top comments (0)