mirror of
https://github.com/motajs/template.git
synced 2026-10-05 11:10:17 +08:00
25 lines
1.2 KiB
Markdown
25 lines
1.2 KiB
Markdown
# `Map.getOrInsert` vs `Map.getOrInsertComputed`
|
||
|
||
所有需要默认值的 Map get 操作,全部使用 `Map.getOrInsert` 或 `Map.getOrInsertComputed`,不得自己写类似下面这种的代码:
|
||
|
||
```ts
|
||
let list = map.get(key);
|
||
if (!list) {
|
||
list = [];
|
||
map.set(key, list);
|
||
}
|
||
return list;
|
||
```
|
||
|
||
- 使用 `Map.getOrInsert`: 数字、短字符串(约 100 以内)、布尔值、空数组、空对象、对象引用
|
||
- 使用 `Map.getOrInsertComputed`: 长字符串、空 Set、空 Map、自定义类构建出的实例(new Class())、有内容的数组、有内容的对象、有内容的 Set、有内容的 Map。
|
||
- 使用哪个都可以: 函数
|
||
|
||
注:如果是直接传引用,那么用 `Map.getOrInsert`,以上对比的是现场构建条件下的性能表现,而非直接传入引用,对比直接传入引用没有任何意义。
|
||
|
||
Benchmark:
|
||
|
||
- [Benchmark1](https://www.measurethat.net/Benchmarks/Show/35964/1/getorinsert-vs-getorinsertcomputed-1)
|
||
- [Benchmark2](https://www.measurethat.net/Benchmarks/Show/35965/1/getorinsert-vs-getorinsertcomputed-2)
|
||
- [Benchmark3](https://www.measurethat.net/Benchmarks/Show/35966/1/getorinsert-vs-getorinsertcomputed-3)
|