DEV Community

Cover image for 🗜️ Optimal Tuple vs Record
All In Developer
All In Developer

Posted on Edited on Originally published at all1n.dev

🗜️ Optimal Tuple vs Record

Should a function return a tuple or an object? Most of the time it’s a matter of taste, but there’s one angle people rarely look at: what each one costs in your production bundle after minification.


👉 Continue reading on my blog: 🗜️ Optimal Tuple vs Record

Top comments (3)

Collapse
 
alexander89 profile image
Alexander Halemba •

To be honest with you, I use both of them where it makes sense.

If you return the tuple [number, number, number, number] in your API, it is easyer to mix them up than 4 fields with a meaningful name.

I won't say that you should not use tuples. const [x, y, z] = vec3() is a good example where everyone will understand this without any docs, but use everything at the right place 😉

Collapse
 
fjones profile image
FJones • • Edited

(Technically, [number, number, number, number] is a quadruple, not a tuple. Tuple specifically refers to two-element lists)

That said, yes, the rule of thumb is to only do this for actual tuples, where the two elements are implicitly clear: one for state, one to modify, and in that order. Otherwise, as much as it bloats the resulting runtime, prefer keys to allow omitting an element declaratively, i.e. by specifying the other two elements you wish to retrieve.

(exceptions exist, such as cases where all elements are mandatory to the consumer in every case)

Collapse
 
all1ndev profile image
All In Developer •

Totally agree 😀 [number, number, number, number] is a no no ))