Shout out to the reduce function literally reducing limitations! 💻 #softwareengineer #javascript

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

JS language developers really challenged themselves to make every detail as unreliable as possible.

cherubinth
Автор

Why are we doing everything to avoid a simple for loop which is clear superior 🎉🎉🎉🎉

dboydomr
Автор

In all honesty, when it comes to learning and sharing expertise, there isn’t need to tag, cite someone else’s content to show it wasn’t comprehensive and then show that you cracked the code of the thing, and let me show how its done. Just a personal opinion. But thank you, something new to me and JavaScript full of these swags anyways.

MuhammadAhmed-pdzu
Автор

Thank you!

Keep it going YouTube recommends!!

Detective_Jones
Автор

I will stick with traditional for loop. It works well in every languages.

KadekRegen
Автор

if reduce is being used, why not just use ternary operator (a>b?a:b) instead of Math.max to save one function call to Math.max on every iteration of reduce?

StarnikBayley
Автор

It's obviously bad to turn entries of a data array into function arguments.
I don't know the inner workings of JS-engines, but if the calling conventions are in any way similar to the C calling convention then you can only pass a small number of arguments through registers and most will be copied to the stack frame of the caller. You'll introduce an additional copy of the data unecessarily, you'll mess up the stack and worsen your cache hit rates

Leosch
Автор

It's probably because Math.max recurses and that's what's causing the error, not spreading the array. If that was the issue you could just use Function.prototype.apply to pass an array as arguments.

Since you're only comparing two numbers in your implementation, it would yield better performance if you just compared them a > b instead of calling Math.max.

kovacsemod
Автор

Why can't the JS max function just take an array as parameter...

PatatorSupelec
Автор

There is problem with your code too, if we passed empty array, it will throw type error, always use initial variable with reduce, in this case -inf

AhmedRakan-yv
Автор

Give any js dev a syntax for making life easier will always misuse it

flamendless
Автор

If you have an array that big you really should be using a simple for loop as reduce will copy that array, leading to worse performance

TheKnocKY
Автор

Reduce is fine but it can lead to massive performance issues with large arrays if you spread a new object or array on every iteration

brettbuchanan
Автор

Your code works only if the maximum is a positive number. In case the maximum can be negative, reduce must be initialised with negative infinity 🙂‍↔️

m____
Автор

The limitation is JavaScript itself. 😅

LucasLocatelli
Автор

Reduce creates a zillion copies. Just use a plain old for loop

QckSGaming
Автор

Javascript has arr.reduce(Func) but doesn't have a simple arr.max(Func) function? Oh god...

ronsijm
Автор

Sometimes I feel like the last person who likes verbose syntax :D Like, I don't see anything wrong with just looping over the array and getting the largest element. Most languges optimize loops under the hood anyways. Obviously, getting a max is a rather trivial example, but still, is this convoluted syntax really necessary?

Chronologist
Автор

Im stupid, so i ask. How does the overhead of the reduce function compare to normal for loops and for each loops?
Is there a sizable diffrence?
Also, does the content matter? Like comparing numbers vs comparing objects with numbers?

Solid_Fuel
Автор

why does arr.reduce(Math.max) not work?

miezekatze
join shbcf.ru