const oddItems = arr => arr.filter((_, i) => i & 1 === 1);
Enter fullscreen mode
Exit fullscreen mode
Ret...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Not looked for faster methods yet, but the current one seems overly verbose. Also, what is
which
for?Binary filters are even faster than the modulo:
Faster still if you omit the unnecesary
=== 1
- it's about even on Firefox, but consistently quicker on Chrome (10-15%) - linkMuch faster again (on all tested browsers):
Hi Jon Randy,
I updated the benchmark on hasty - impressive improvement!
I updated the code and the article.
Thank you for the improved code.
Cheers!
Thank you!
If you are already using a for loop, you can also jump 2 steps on every iteration:
Haha... yeah - oops
You might want to make the benchmark call the functions as well as define them! :P
wouldn't jumping 2 steps ensure only odd positional, but not values?
(I just assume an unsorted array of random values)
Odd positional is what we're after
Hi Alex,
I checked the performance on hasty and indeed much faster. I updated the code and the article.
Thank you!
Thank you for your contribution. The "which" is a copy paste error :D
If you have any performance improvements, please share them and I'll happily adjust the code and the article.
Cheers,
Martin