Skip to content

驻留值

🌐 Interning values

InternMapInternSet 类分别扩展了原生 JavaScript 的 Map 和 Set 类,通过在确定键的相等性时绕过 SameValueZero 算法,允许使用日期和其他非原始类型的键。d3.groupd3.rollupd3.index 使用 InternMap 而不是原生 Map。

🌐 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]
]);

示例 · 来源 · 根据指定的 [key, value] 条目的 iterable 构建一个新的 Map。键使用指定的 key 函数进行内部化,对于非原始值,默认为 object.valueOf。例如,要获取由给定日期作为键的值:

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")
]);

示例 · 来源 · 根据指定的可迭代对象构建一个新的集合。使用指定的key函数对值进行驻留,该函数对于非原始值默认使用object.valueOf。例如,要查询给定的日期:

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