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

示例 · 源代码 · 构造一个新的 Map,给定指定的 [key, value] 项的可迭代对象。keys 使用指定的 key 函数进行驻留,对于非原始值,该函数默认为 object.valueOf。例如,检索由给定日期键入的值:

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

示例 · 源代码 · 根据指定的可迭代值构造一个新的 Set。这些值使用指定的 key 函数进行驻留,对于非原始值,该函数默认为 object.valueOf。例如,查询给定日期:

¥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