介绍下 Set、Map、WeakSet 和 WeakMap 的区别?

1. 概念解释

1.1 Set

Set
The Set object lets you store unique values of any type, whether primitive values or object references.

Description
Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.

如上是官方的解释,概括起来就是提供一个存储唯一值的容器,Set 会对其内部的存储值去重。
常用的方法有:has,add,delete

1.2 Map

Map
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

Description
A Map object iterates its elements in insertion order — a for...of loop returns an array of [key, value] for each iteration.

提供一个键值对的存储容器,类似于字典,排序方式保持插入顺序。
提供的方法有:has,delete,get,clear

1.3 WeakSet

WeakSet
The WeakSet object lets you store weakly held objects in a collection.

Description
WeakSet objects are collections of objects. Just as with Sets, each object in a WeakSet may occur only once; all objects in a WeakSet's collection are unique.

The main differences to the Set object are:

WeakSets are collections of objects only. They cannot contain arbitrary values of any type, as Sets can.
The WeakSet is weak, meaning references to objects in a WeakSet are held weakly. If no other references to an object stored in the WeakSet exist, those objects can be garbage collected.
Note: This also means that there is no list of current objects stored in the collection. WeakSets are not enumerable.

提供一个弱引用对象的存储集合,成员只能是对象,且在成员没有其他引用时,可以被回收。
不能被遍历。
存储null ,数值或字符串时会报错。

1.4 WeakMap

WeakMap
The WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.

You can learn more about WeakMaps in the WeakMap object guide (under Keyed collections).

Description
Keys of WeakMaps are of the type Object only. Primitive data types as keys are not allowed (e.g. a Symbol can't be a WeakMap key).

Why WeakMap?
A map API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the four API methods. Setting elements on this map would involve pushing a key and value onto the end of each of those arrays simultaneously. As a result, the indices of the key and value would correspond to both arrays. Getting values from the map would involve iterating through all keys to find a match, then using the index of this match to retrieve the corresponding value from the array of values.

Such an implementation would have two main inconveniences:

The first one is an O(n) set and search (n being the number of keys in the map) since both operations must iterate through the list of keys to find a matching value.
The second inconvenience is a memory leak because the arrays ensure that references to each key and each value are maintained indefinitely. These references prevent the keys from being garbage collected, even if there are no other references to the object. This would also prevent the corresponding values from being garbage collected.
By contrast, native WeakMaps hold "weak" references to key objects. As such native WeakMaps do not prevent garbage collection, which eventually removes references to the key object. "Weak" references also avoid preventing garbage collection of values in the map. WeakMaps can be particularly useful constructs when mapping keys to information about the key that is valuable only if the key has not been garbage collected.

Because the references are weak, WeakMap keys are not enumerable. There is no method to obtain a list of the keys. If there was, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a Map.

提供一个Key值时弱引用对象的键值对集合容器。
Key 只能是对象。
不能被遍历,且无法获取Key值集合。

2. 综述

Set 和 Map,提供数据存储容器,Set存储单个值,Map存储键值对,Set 值不能重复,Map Key值不能重复。
WeakSet ,WeakMap 是弱引用对象集合,WeakSet 的值和 WeakMap 的键值仅能存储对象引用,且可以在无其他引用时被回收。而这一特点也导致了其不能执行遍历操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值