News for hackers. Stuff that matters.
Re:Sparse Array Destructuring in JavaScript by bastawhiz on Wednesday November 16, 2022 @11:16PM (#33634899)
It's called "array destructuring" because you're destructuring _arrays_, but this is treating arrays like _tuples_. If you're storing more than 3 (really 2) pieces of information about a single thing in a tuple, you're almost certainly misusing tuples. Why would you store a payment record, for instance, in a tuple of the form `[customerEmail, cardLast4, amount, currency, timestamp]` when you could just use an object, have consistent names that self-document the data structure, and never need to think about the ordering of elements in tuples?

This is really only even marginally appropriate if you want to do something like this:

  return Object.entries(myObject)
    .filter(([, value]) => value % 2 === 0)
    .map(([key, value]) => `${key} has even value ${value}`);
Skipping the key in the `filter` call is probably straightforward enough that nobody is going to be confused about what's happening. But if you're doing this for a tuple of any significant length, you're just papering over a much more serious problem that's already present in your code.
Re:Sparse Array Destructuring in JavaScript by postalrat on Wednesday November 16, 2022 @11:38PM (#33635038)
The rare case for me is when you really care about size. The destructured variables can be renamed by the minifier while an object will keep the property names.
Re:Sparse Array Destructuring in JavaScript by bastawhiz on Wednesday November 16, 2022 @11:57PM (#33635145)
That's where gzip or brotli come in. In a lot of cases the number of bytes over the wire hardly changes.
Re:Sparse Array Destructuring in JavaScript by tonto on Thursday November 17, 2022 @12:36AM (#33635383)
maybe interesting: the obj destructuring may be a bit faster than array destructuing. setup a small benchmark here https://jsbench.me/eolakmvsor/1

just storing the data and accessing it as a tuple seems to not be the significantly slow part, but instead its destructuring the array that can be a bit slow. i remember seeing some threads with e.g. useState hooks in react needing array destructuring optimizations, but appears to still be a bit slower https://docs.google.com/document/d/1hWb-lQW4NSG9yRpyyiAA_9Kt...