0

I'm in the process of porting some code that used the hashmap library to ES6 Map. However, I'm running into an issue that my existing maps are keyed with arrays. Come lookup time, equivalent arrays are generated and used as lookup keys. Using HashMap, this was fine, since complex keys are hashed in a way that's equivalent to deep comparison. ES6's Map, on the other hand, performs strict equality comparison on the keys.

My old method of looking up keys:

var h = new HashMap();
var k = [1,2,3];
h.set(k, 'foo');
h.has([1,2,3]) // returns true

Under ES6, that method doesn't work, due to strict equality comparison.

var m = new Map();
var k = [1,2,3];
m.set(k, 'foo');
m.has([1,2,3]) // returns false

Is there a way to force a given comparison function, or overload get() in a way that will look up complex keys by deep comparison instead of strict equality? I haven't been able to create a Map subclass that does so without a linear search through the keys, completely destroying any efficiency gains I'd've gotten from using a native type in the first place.

sehrgut
  • 209
  • 1
  • 13

0 Answers0