Should you 'pre-allocate' the array with new Array(n)? yes
https://jsbench.me/45l02v2h9e/1
Replace newlines with empty string, replaceAll slightly faster (though has less browser support)
https://jsbench.me/a2l02vz8l8/1
Count the number of instances of a thing in an array using a number of sample codes from long stack overflow thread
https://jsbench.me/2wkzzagdu3/1
Ultimately some bit twiddling wins
https://jsbench.me/z4l0h7kso6/1
new Map() about 1.5x faster than Object.fromEntries
UInt8Array is faster https://jsbench.me/gll0k715zn/1
toLocaleString a bit slower than sort with no args https://jsbench.me/ool0mt6kwf/2
Not using a regex can be about 35% faster in Chrome, in firefox the regex is a bit faster
- https://jsbench.me/gpl0pyj08n/1 (10 cigar ops)
- https://jsbench.me/gpl0pyj08n/3 (100 cigar ops)
- https://jsbench.me/gpl0pyj08n/3 (1000 cigar ops)
Even if you save the regex result and re-run it multiple times, not using a regex is still a bit faster
- https://jsbench.me/gpl0pyj08n/4 (10 cigar ops)
- https://jsbench.me/gpl0pyj08n/5 (100 cigar ops)
- https://jsbench.me/gpl0pyj08n/6 (1000 cigar ops)
A task to fill up a 'buffer' with a bunch of stuff, where there might be an unknown number of things Test array, UInt8Array, string https://jsbench.me/gll0k715zn/1
Similar one but fills up a string, but with a constant number of things. The result is dependent on size. On small size strings, string append faster. On larger strings, textdecoder faster
- small (len 100) https://jsbench.me/7ql0r1tlx1/1 string append wins
- med (len 1000) https://jsbench.me/7ql0r1tlx1/2
- large (len 10000) https://jsbench.me/7ql0r1tlx1/3 textdecoder wins
Have to be careful with blowing the stack with concat but for a small number of items, [].concat(...items) can be faster than items.flat()
https://jsbench.me/1el0ugfw9g/1
for of loop can be faster
- https://jsbench.me/xnl0ws0kfm/1 (10 entries in map)
- https://jsbench.me/xnl0ws0kfm/2 (1000 entries in map)
- https://jsbench.me/xnl0ws0kfm/3 (1000 entries in map)
seen here https://stackoverflow.com/questions/69145734/fastest-way-to-loop-through-map-keys
the forEach can be faster
Not really any significant differences here
https://jsbench.me/73l1p556jq/1
You can combine many rect drawing commands into a single one with Path2D, and it can be significantly faster than many individual fillRect calls https://jsfiddle.net/antonphp/1ghLn4a9/57/
https://jsbench.me/eolakmvsor/1
This was also noted by v8 team in relation to React hooks and actively optimized for performance, but destructuring array appears to still be a little bit of a bottleneck https://docs.google.com/document/d/1hWb-lQW4NSG9yRpyyiAA_9Ktytd5lypLnVLhPX9vamE/edit