Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
A hell of a lot cleaner, in theory a bit slower too. filter allocates, fills and returns an array, which we're then instantly throwing away after getting its length. reduce functions more like a for loop, iterating over each element and accumulating a sum.
That's in theory though. JS performance is fickle thing, and on perf.link it seems your filter method is both cleaner looking, and faster for arrays with fewer than 10000 items. Perf.link link
Go figure! Good to know
What we really want is something like ruby's count, which only returns the filtered size...
You can use this property of booleans to implement an exclusive or:
That's clever. Thank you!
Or count things cleanly...
Now I think about it,
filter
withlength
would be simpler in both casesA hell of a lot cleaner, in theory a bit slower too.
filter
allocates, fills and returns an array, which we're then instantly throwing away after getting its length.reduce
functions more like afor
loop, iterating over each element and accumulating a sum.That's in theory though. JS performance is fickle thing, and on perf.link it seems your
filter
method is both cleaner looking, and faster for arrays with fewer than 10000 items.Perf.link link
Go figure! Good to know
What we really want is something like ruby's
count
, which only returns the filtered size...😎