JS/TS performance snippets
- Description
- Snippets and performance hints.
- Tags
- Web developmentWeb performance
- Last updated:
Prefer maps to objects for dynamic data
Objects aren’t ideal for manipulation, especially in Typescript. If you add a key, the typings get thrown of. Prefer a JS Map.
Checking if items is in list
new Set(arr).has(item)
is actually much faster than Array.includes()
for checking the existence of primitives in a set. Prefer that.
If you need a function for lookup, use Array.some()
Delete single item from list
Just as above. Array.from(new Set(arr).delete(key))
is faster than removing it with filter. Also works only on primitives.