One discovery I've made is that using new Array is significantly faster than Array.from or push()[0].
Could you share any similar insights or lesser-known performance optimizations you've come across?
[0] https://www.measurethat.net/Benchmarks/ShowResult/506720
• Learn Rust! When I learned how memory worked, it made me a much better JS dev.
• Check out Casey Muratori's courses on performance. Some are free on YouTube.
• Start from scratch with a data-oriented approach. What bits do you need to move around, and why? What's the best way to store and shape those bits?
• Can/should you use WASM instead?
Never use .forEach(), always use a for loop. Same applies to methods like .map().
Some calculations are just much more expensive to do than others. Exponentiation for example is much more expensive than multiplication. You want to write your code in a way that not only considers the total number of operations, but also minimises expensive operations.
Caching can help if you're doing repetitive computations.
If you're absolutely pushing performance to the max you will probably want to look at doing some clever stuff with WebGL2, WASM or Web Workers. Wherever you can avoid this though I would recommend it because it will make your code much more complicated and won't even necessarily out perform because of overheads.
Edit: Also check out this guy on YT, https://www.youtube.com/watch?v=WLwTlC1R2sY He does some really interesting deep dives into JS performance.
https://github.com/prettydiff/wisdom/blob/master/performance...
The techniques mentioned are stupid fast to the fewest milliseconds, but most JavaScript developers find this incredibly unpopular.
always remember the fastest code is the code that is not executed at runtime.
This makes no sense to me, since checking type is supposedly the first step of each equality operator’s algorithm, but this is the result JSBench keeps returning me.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
This isn't necesarrily unknown, but knowing that there's a cost associated with promises and async/await will get you pretty far in terms of performance. I remember the idea first clicking when I watched this : https://www.youtube.com/watch?v=SMBvjmeOotA
Use chrome profiler