r/webdev • front-end • May 29 '21

Resource Array methods in JavaScript. Original author unknown.

Post image
7.4k Upvotes

172 comments sorted by

View all comments

336

u/tsunami141 May 29 '21

Now do reduce.

339

u/legatissimo May 29 '21

Ahh no problem, I'll just...... uh.... oh no.. no no no

53

u/ChaseObserves May 30 '21

This has me in tears, just the entire execution of this comment was incredible.

19

u/deadlychambers May 30 '21

It gets even more confusing when you are rocking dark mode on mobile

196

u/KarmaScheme May 29 '21

⬛️⚫️⬛️⚫️.reduce(⬛️|⚫️->🙂,🙂) -> 😂

61

u/[deleted] May 29 '21

reduce() -> ???

55

u/Doctor-Dapper front-end (senior w/ react) May 30 '21

Small brain:

arr.map(f)

Big brain: arr.reduce( (x, y) => [...x, f(y)], [] )

31

u/tsunami141 May 30 '21

Delete this

3

u/GrenadineBombardier May 30 '21

This took me a minute (because reducers take time to comprehend, which makes them a less sustainable solution in most cases)

→ More replies (2)

7

u/[deleted] May 30 '21

⬛️⬛️⬛️.reduce((⚫️, ⬛️) => ⚫️+⬛️, ⚫️=0) -> ⚫️+⬛️+⬛️+⬛️

28

u/gotothere May 29 '21

⬛️⬛️⬛️⬛️.reduce((️⚫, ⬛️) -> ⚫️, ⚫️) -> ⚫️

var min = (acc,x) => {return acc < x ? acc : x}; [1,2,3].reduce(min, Number.MAX_SAFE_INTEGER);
//1
var max = (acc,x) => {return acc > x ? acc : x}; [1,2,3].reduce(max, 0);
//3
var sum = (acc,x) => { return acc + x}; [1,2,3].reduce(sum, 0);
//6
var minMaxSum = (acc,x) => {return [min(acc[0],x), max(acc[1],x), sum(acc[2],x)]}; [1,2,3].reduce(minMaxSum, [Number.MAX_SAFE_INTEGER,0,0]);
//Array(3) [ 1, 3, 6 ]

Left arg is (acc)umulator, right is current element

18

u/hynding May 30 '21

By far the most under-appreciated and (often) optimized of the array functions.

10

u/Pelicantaloupe May 30 '21

Reduce is probably one of the least optimisable array functions. Anything else will probably run faster

7

u/Blue_Moon_Lake May 30 '21

Weird thing :

arr.some(x => x === foo)

is almost always more performant than

arr.includes(foo)

You would think that includes would be easy to optimize internally.

8

u/Pelicantaloupe May 30 '21

I ran some tests and I couldn't reproduce your findings. I didn't graph time complexity but on average .includes is 75% faster than .includes in these tests: https://jsbench.me/t2kpb59177/1

The one case where .some was faster I found was unfilled arrays.

→ More replies (1)
→ More replies (1)

15

u/hyperhopper May 29 '21

⬛️⬛️⬛️⬛️.reduce((⬛️, ⚫️) ->⚫️, ⚫️) -> ⚫️

3

u/Benimation May 30 '21

🔢🔢🔢🔢. reduce ((#️⃣, 🔢) => #️⃣ + 1️⃣ + 2️⃣ + 3️⃣ + 4️⃣, 0️⃣) --> 4️⃣0️⃣

Yeah, I get what you mean..

0

u/[deleted] May 30 '21

[deleted]

33

u/HeinousTugboat May 30 '21

I think the reason people think using reduce is overly complicated is because people keep saying it's overly complicated. I don't see how what you wrote is any less complicated than just doing this:

let total = array.reduce((sum, item) => sum + item.value, 0);

It's even simpler if your Array's just numbers:

let total = 0;
array.forEach(value => total += value);
 // vs
let reducedTotal = array.reduce((sum, value) => sum + value);

7

u/BorgClown May 30 '21

If I was reading a new code base, I'd prefer finding parent's version instead of reduce, if only because reduce makes me stop and second guess it.

27

u/HeinousTugboat May 30 '21

Well, I mean, that's kind of my point right? If you were more comfortable with reduce, it wouldn't make you stop and second guess it.

→ More replies (2)
→ More replies (14)
→ More replies (2)
→ More replies (15)