Skip to content

Interning values

The InternMap and InternSet classes extend the native JavaScript Map and Set classes, respectively, allowing Dates and other non-primitive keys by bypassing the SameValueZero algorithm when determining key equality. d3.group, d3.rollup and d3.index use an InternMap rather than a native Map.

new InternMap(iterable, key)

js
const valueByDate = new d3.InternMap([
  [new Date("2021-01-01"), 42],
  [new Date("2022-01-01"), 12],
  [new Date("2023-01-01"), 45]
]);

Examples · Source · Constructs a new Map given the specified iterable of [key, value] entries. The keys are interned using the specified key function which defaults to object.valueOf for non-primitive values. For example, to retrieve a value keyed by a given date:

js
valueByDate.get(new Date("2022-01-01")) // 12

new InternSet(iterable, key)

js
const dates = new d3.InternSet([
  new Date("2021-01-01"),
  new Date("2022-01-01"),
  new Date("2023-01-01")
]);

Examples · Source · Constructs a new Set given the specified iterable of values. The values are interned using the specified key function which defaults to object.valueOf for non-primitive values. For example, to query for a given date:

js
dates.has(new Date("2022-01-01")) // true