Skip to content

Instantly share code, notes, and snippets.

@cmdcolin
Last active March 15, 2023 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmdcolin/ef57d2783e47b16aa07a03967fd870d8 to your computer and use it in GitHub Desktop.
Save cmdcolin/ef57d2783e47b16aa07a03967fd870d8 to your computer and use it in GitHub Desktop.
Benchmarks

Array fill

Should you 'pre-allocate' the array with new Array(n)? yes

https://jsbench.me/45l02v2h9e/1

Replace whitespace in string

Replace newlines with empty string, replaceAll slightly faster (though has less browser support)

https://jsbench.me/a2l02vz8l8/1

Frequency table

Count the number of instances of a thing in an array using a number of sample codes from long stack overflow thread

https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements/59225909#comment125913203_59225909

https://jsbench.me/2wkzzagdu3/1

Ultimately some bit twiddling wins

Object.fromEntries([]) vs new Map([])

https://jsbench.me/z4l0h7kso6/1

new Map() about 1.5x faster than Object.fromEntries

Using UInt8Array instead of adding to string for large list of items

UInt8Array is faster https://jsbench.me/gll0k715zn/1

Sort strings

toLocaleString a bit slower than sort with no args https://jsbench.me/ool0mt6kwf/2

CIGAR strings

Not using a regex can be about 35% faster in Chrome, in firefox the regex is a bit faster

Even if you save the regex result and re-run it multiple times, not using a regex is still a bit faster

Fastest buffer filling

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

Flatten array with .flat() vs .concat

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

Accessing values of map

for of loop can be faster

Accessing keys of map

seen here https://stackoverflow.com/questions/69145734/fastest-way-to-loop-through-map-keys

the forEach can be faster

Filling up nested object in a sparse array

Not really any significant differences here

https://jsbench.me/73l1p556jq/1

Drawing many rectangles on canvas

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/

Destructuring an array can be somewhat slow

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment