internmap

中文文档
2.0.3发布于 2021/09/20ISCES Module零依赖近 30 天下载 3.7亿

Map and Set with automatic key interning

原始使用文档

InternMap

For live examples, see https://observablehq.com/@mbostock/internmap.

If you use dates as keys in a JavaScript Map (or as values in a Set), you may be surprised that it won’t work as you expect.

dateMap = new Map([
  [new Date(Date.UTC(2001, 0, 1)), "red"],
  [new Date(Date.UTC(2001, 0, 1)), "green"] // distinct key!
])
dateMap.get(new Date(Date.UTC(2001, 0, 1))) // undefined!

That’s because Map uses the SameValueZero algorithm to determine key equality: for two dates to be considered the same, they must be the same instance (the same object), not just the same moment in time. This is true of the equality operator, too.

{
  const date1 = new Date(Date.UTC(2001, 0, 1));
  const date2 = new Date(Date.UTC(2001, 0, 1));
  return date1 === date2; // false!
}

You can avoid this issue by using primitive values such as numbers or strings as keys instead. But it’s tedious and easy to forget to coerce types. (You’ll also need to do the inverse type conversion when pulling keys out of the map, say when using map.keys or map.entries, or when iterating over the map. The inverse above is new Date(key). Also, if you forget to coerce your key to a number when using map.get, it’s easy not to notice because the map won’t throw an error; it’ll simply return undefined.)

numberMap = new Map([[978307200000, "red"]])
numberMap.get(978307200000) // "red"
numberMap.get(new Date(978307200000)) // undefined; oops!

Wouldn’t it be easier if Map and Set “just worked” with dates? Or with any object that supports object.valueOf?

Enter InternMap. Interning refers to storing only one copy of each distinct key. An InternMap considers two Date instances representing the same moment to be equal, storing only the first instance.

map = new InternMap([
  [new Date(Date.UTC(2001, 0, 1)), "red"],
  [new Date(Date.UTC(2001, 0, 1)), "green"] // replaces previous entry
])
map.get(new Date(Date.UTC(2001, 0, 1))) // "green"
[...map.keys()] // [2001-01-01]

InternMap extends Map, so you can simply drop it in whenever you’d prefer this behavior to the SameValueZero algorithm. Because InternMap calls object.valueOf only for non-primitive keys, note that you can pass primitive keys, too.

map.get(978307200000) // "green"; this works too!

InternMap keeps only the first distinct key according to its associated primitive value. Avoid adding keys to the map with inconsistent types.

map2 = new InternMap([
  [978307200000, "red"], // danger!
  [new Date(Date.UTC(2001, 0, 1)), "blue"]
])
map2.get(new Date(Date.UTC(2001, 0, 1))) // "blue"; this still works…
[...map2.keys()] // [978307200000]; but the key isn’t a Date

While InternMap uses object.valueOf by default to compute the intern key, you can pass a key function as a second argument to the constructor to change the behavior. For example, if you use JSON.stringify, you can use arrays as compound keys (assuming that the array elements can be serialized to JSON).

map3 = new InternMap([
  [["foo", "bar"], 1],
  [["foo", "baz"], 2],
  [["goo", "bee"], 3]
], JSON.stringify)
map3.get(["foo", "baz"]) // 2

There’s an InternSet class, too.

set = new InternSet([
  new Date(Date.UTC(2000, 0, 1)),
  new Date(Date.UTC(2001, 0, 1)),
  new Date(Date.UTC(2001, 0, 1))
])

版本与发布记录

internmap 共发布 6 个版本,首次发布于 2021/01/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2021/09/205 年前
2.0.22021/09/195 年前
2.0.12021/06/065 年前
2.0.02021/06/045 年前
1.0.12021/03/115 年前
1.0.02021/01/225 年前

关于 internmap 的常见问题

internmap 是什么?

Map and Set with automatic key interning

如何安装 internmap?

在项目目录运行 npm install internmap,或使用 pnpm add / yarn add / bun add。

internmap 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/internmap 是否存在。

internmap 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

internmap 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

internmap 可以商用吗?

internmap 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

internmap 的下载量有多少?

根据 npm 官方下载统计,internmap 在 2026/10/06 – 2026/11/04 这 30 天内被下载 372,633,298 次,日均约 12,421,110 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

internmap 还在维护吗?

最新版本 2.0.3 发布于 2021/09/20(5 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/01/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。