News for hackers. Stuff that matters.
Re:An efficient way to make hash maps with insertion ordering by arsome on Thursday July 01, 2021 @12:14PM (#27700719)
I'm aware of them, but the problem with that is you can only compare an object by instance if I recall correctly. There's no way to override the hashing and equality check that I could find, meaning even for simple objects you're basically forced to serialize your keys in some way still, which is disgustingly inefficient and slow for no good reason.
Re:An efficient way to make hash maps with insertion ordering by doteka on Thursday July 01, 2021 @01:11PM (#27701522)
So much this. I remember being so excited that Map and Set were added to the language, only to find that they were worse than useless for objects. At least Set can be handy in a pinch, but I have never seen an actual usecase for Map that isn’t solved better with plain old JS objects. Curious if anyone else has?
Re:An efficient way to make hash maps with insertion ordering by upzylon on Thursday July 01, 2021 @01:29PM (#27701795)
There definitely are valid use cases for a Map, and even more now that we have WeakMaps (e.g. associating data DOM nodes). Or mapping functions to values, e.g. something like this:

  const returnCache = new Map<() => any, any>()

  const memoized = <T extends () => any>(f: T): ReturnType<T> => {
    if (returnCache.has(f)) return returnCache.get(f)
    const v = f()
    returnCache.set(f, v)
    return v
  }
Re:An efficient way to make hash maps with insertion ordering by gildas on Thursday July 01, 2021 @04:01PM (#27703952)
> I have never seen an actual usecase for Map that isn’t solved better with plain old JS objects. Curious if anyone else has?

With a Map, you can easily and efficiently associate data with DOM nodes without changing them.