Map vs WeakMap
const map = new Map()
map.set(1, 'one')
console.log(map.get(1))
const weakMap = new WeakMap()
// this will fail with an error:
// weakMap.set(1, 'one')
// console.log(weakMap.get(1))
Noticed this gotcha the other day. For some reason WeakMap
can’t have integers as keys. After years of using WeakMap
I guess I’ve only ever used objects as keys and assumed it could just have keys of any type. Using a Map
instead solves the problem, but you’ll need to be careful to manager your references to properly clear the Map
. Anyway, just one of those things… Maybe I’ll write more about it when I have some time to dig deeper.