2

1 line of code: How to get every even item of an Array

 2 years ago
source link: https://dev.to/martinkr/1-line-of-code-how-to-get-every-even-item-of-an-array-3pl3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for 1 line of code: How to get every even item of an Array
martin krause

Posted on Nov 10

1 line of code: How to get every even item of an Array

One line of code (JS) (16 Part Series)

const evenItems = (arr, out = [], i) => { for (i = 0; i < arr.length; i = i + 2) out.push(arr[i]); return out; };
Enter fullscreen modeExit fullscreen mode

Returns an array which contains every even item of the original array.


The repository & npm package

You can find the all the utility functions from this series at github.com/martinkr/onelinecode
The library is also published to npm as @onelinecode for your convenience.

The code and the npm package will be updated every time I publish a new article.


Happy coding and consider to buy me a coffee

Photo by zoo_monkey on Unsplash


Discussion (7)

pic

CollapseExpand

Or you use the fastest possible version:

const evenItems = (arr, out = [], i) => { for (i = 0; i < arr.length; i = i + 2) out.push(arr[i]); return out; };
Enter fullscreen modeExit fullscreen mode

Comment button Reply

CollapseExpand

I mean, you could just use a filter, which is faster and from my point of view is also more readable:

const evenItems = array => array.filter((_, index) => index % 2 === 0);
Enter fullscreen modeExit fullscreen mode

Cheers!

Comment button Reply

CollapseExpand

Author

Nov 10

Hi Luke,

you are right filter is more intuitive, but it is actually slower for me.

Cheers!

Comment button Reply

CollapseExpand

Author

Nov 10

thank you for taking the time to contribute, I put both functions to the performance benchmark on hasty and I'm going to update the article with your suggestion!

Cheers!

Comment button Reply

CollapseExpand

You forgot to run the code in that benchmark, so you're only testing the declaration of both functions, not the actual function running ... here you have an actual comparison. With filter it is faster.

Thread

Thread

Author

Nov 10

Hi Luke,
yes you are right :D Here is the updated benchmark on hasty - filter is much slower for me.

Thread

Thread

Try moving the funciton declarations to the global block (you only need to declare them once and then run them). Maybe is slower in non-chromium browsers, I'm testing on Chrome.

Edit: Tried it in Safari and performance sucks there. Chromium makes optimizations in this kind of algorithms that other browsers don't. From a logic stand point the for approach effectively should run in half the time because is skipping the odd indexes instead of filtering them. Still I would prefer that other browsers optimize my code as Chromium does so I can write simple code and it gets optimized for me.

Comment button Reply


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK