r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

2

u/Shaper_pmp Mar 19 '19

I never understand why people feel the need to populate an array with a sequence of consecutive values. I've spent over 20 years writing javascript and never once encountered a legitimate use-case for doing it.

The only reason I can imagine is if someone wanted to "iterate X times" and was allergic to the simple for loop, so instead they're defining, reserving and populating an entire array in memory just so they can iterate over its array indices and then immediately throw it all away again... but that's pants-on-head crazy code.

Genuinely curious, in case I'm missing something...

1

u/dai-shi Mar 19 '19

I'd agree that we should avoid allocating memory if we could. I too don't use this form in production code. It's better to use just an iterator `Array(n).keys()` not an allocated array.

The only reason I'd require an array is when the function I call doesn't accept an iterator but only an array. I often use array.map, and it's handy to have an allocated array, mostly for tooling not production code.

iota usually take the second and third arguments, and without it, the usage is limited.

1

u/Shaper_pmp Mar 20 '19

Instantiating a copy of an iterator function is probably a slightly better solution than allocating an entire array just to throw it away again, but you didn't answer the core question - why bother instantiating any whole object (either an array or an Iterator), instead of using a simple for loop with a single integer counter?

I'm still unclear how

iota(5).forEach(i => {...})

is clearer, or more efficient, or more readable, or more maintainable than

for(let i=0; i<5; i++) {...}

... let alone if you want to do something weird like start from a non-zero number or count up in 2s.

All those things are possible using your iota method, but every single one is more complex or fiddly and less readable than a simple for loop.

What do people have against just using for?

1

u/dai-shi Mar 20 '19

I think it’s just everyone’s preference whether to use array.forEach or for-loop. My programming background is something that doesn’t have loops and I even tend to avoid array.forEach. This is an extreme case but I understand some people prefer arrays to loops.

1

u/Shaper_pmp Mar 20 '19

I think it’s just everyone’s preference whether to use array.forEach or for-loop.

It's not just preference though - instantiating an array costs time and memory that's completely unnecessary, and is code that's non-idiomatic and confusing.

Arrays are for holding and iterating over values. for is for iterating over numbers.

Abusing arrays and constructing/garbage-collecting objects in memory just to do what a simple for loop could do more cheaply and readably is objectively bad code.

It sounds like you're just saying "I have no real reason for taking this sub-optimal path other than I don't understand the trade-offs involved and don't like for loops for no good reason".

If that's the case then I would strongly advise you to discard any phobias you have about unfamiliar constructs and concentrate on learning to write idiomatic code in a new language, or you're basically missing half the benefit of learning one.

1

u/kybernetikos Mar 20 '19

Let's say you want to sort an array without modifying it. Well you can't, but you can do something close:

function sortIndexes(arr) { 
    return [...new Array(arr.length).keys()].sort((a, b) => arr[a] - arr[b]) 
}

Now you haven't modified the original array, but you have an array of indexes in the right order. Manipulating indexes rather than the original array can be a very useful technique.

1

u/Shaper_pmp Mar 20 '19 edited Mar 20 '19

That is an example of a scenario, but honestly it seems a pretty tortured one. Why wouldn't you simply shallow-clone the original and use Array.sort()?

Also your function doesn't actually sort anything - it just populates a completely new array with the numbers from 0->arr.length.

Sure you could take that array and subsequently sort it, but your function is badly misnamed because as written it doesn't do anything useful.

Also I'm sceptical it handles sparse arrays properly (on mobile, so I can't check).

2

u/kybernetikos Mar 20 '19

You should run the code and try it.

1

u/Shaper_pmp Mar 20 '19

Sorry - I missed the .sort() when I was scrolling horizontally on mobile. My bad.

Fair point that it's actually doing the sorting, but as I said: why not just do [...arr].sort() to get a shallow copy of the actual array that has the actual values already helpfully sorted, instead of mucking about with the indices in a separate array?