Does JavaScript loop performance matter?

preview_player
Показать описание
tools over rules
Рекомендации по теме
Комментарии
Автор

Always readability over (in most cases) micro-optimizations. Computers will always get faster but developers never learn to read better 😂

jaredrethman
Автор

My 2 personal "rules"/"responsibilities" as a developer: 1) make the code work, 2) make the code maintainable. I'll always prefer a more maintainable option/syntax over performance until it is an issue.

wolfphantom
Автор

Came for the title, leaving to go listen to Box Car Racer

matthew_bub
Автор

DX should be important, but if performance is really a concern then opt in for workers and wasm.

ManvendraSK
Автор

That's a great website, ill have to use that more often. The difference between browsers is crazy

mike-
Автор

People unironically arguing about this in PRs when your data has 100 items

anarchymatt
Автор

I always use for loops to loop over my 3 objects because of performance. :-)

bmehder
Автор

Don't optimize before you need to unless you really foresee it becoming an issue. And even then, it appears you need to run a benchmark in your code to determine the fastest implementation and dynamically change the implementation depending on those results since each js engine is different

DontFollowZim
Автор

Performance in JavaScript is tough. It depends on how well you understand how JavaScript handles garbage collection. A lot of the cool new toys like spread operators or helpers like .reduce .map are sometimes going to be slower than a for loop or doing it manually. It can actually make a difference. There was an issue for tanstack tables due to the spread operator (that package handles large data sets with tables and uses reduce!). Also, these performance sites are not always the best way to measure performance (which you mentioned a bit :D).

brittdanzer
Автор

If the performance matters, then have the server do the grouping. Remember that two users can have wildly different hardware!

codeman-dev
Автор

It doesn’t matter in most cases, but also what’s faster today is not necessarily faster tomorrow.

Once a feature is more widely used, engine developers can use real world data to improve the real world performance of these functions.

feldinho
Автор

afaik classical for loops are even faster than for-of. i will always use reduce though unless i run into perf issues bcs of better dx.

johnnytestarossa
Автор

ahahah so we should put conditions for loops based on browsers for the best performance!!!

gageracer
Автор

If you start chaining some iteration functions they create new arrays each which might be a lot of garbage for large sets afaik

claasdev
Автор

How about raw testing in NodeJS and BunJS? That's more interesting for results.

gabrielmlocik
Автор

i dont have a history in programming and im teaching all my knowledge to my self so my opinion probably wont mater that much but still here it is:
i think that readability is most important, especially when your working in a group/team. So making something like that extremely optimized wont be necessary.

My only question is, when you're optimizing something like that, would you look for the best option that is likely gonna be used, for example optimize for chrome because it has the highest user base (dont know the numbers, just an example) or would you optimize it for every browser?

GckS_Bootfahrer
Автор

If you’re using JS, Fast enough >>> Fastest. If speed is so critical to you then you can always use Go and if even more is required you can always go punish yourself with C++ or Rust.

BlackCodeMath
Автор

For of loop is slower then regular for loo... i use only for loop

nr
Автор

Was unfamiliar with that site. Thanks for showing it!!

I normally run my own tests in an IDE by hand (I mostly work back end and my code doesn’t often run client side).

I feel like when looking at performance in multiple browsers you also need to consider market share.

After a quick search I see Chrome at about 60%. Firefox at about 2%. Safari at about 20%.

Exact details seem to differ from site to site.

I wouldn’t really worry about Firefox at all, and it sure seems like if you’re going to implement this with thought on browser runtime chrome is all that matters … unless you determine browser type and THEN run code unique to each to make each browser perform best for itself.

Also … I would default to a forEach for this rather than the reduce or a raw for loop (or the for of loop you showed). I pretty much only do for loops if there’s a way to exit early to avoid the full N time iterating over all of the data. GroupBy still seems really interesting though!

zero
Автор

It's not that these simplified methods being slower than a for loop matters in individual cases, I think. It's that they obscure the fact that they are looping. You can end up with very slow code without it being obvious why. When you teach yourself to use array methods for everything, you might find it hard to optimize once you really need it. It might be good to always measure execution time for critical stuff even when the data sets are not huge just to see if its reasonable.

mintcar
welcome to shbcf.ru