ES6: Map, WeakMap

ES6 中新加入了Map和WeakMap类用于存储 key - value pair。区别于原来的object, Map和WeakMap的key 可以是any value (objects or primitive values), 而 object 的 key 只能是 string 或者 symbol

Map


Create a map

  • let map = new Map([iterable]);
  • iterable
    • An Array or other iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[1, 'one'], [2, 'two']]). Each key-value pair is added to the new Map; null values are treated as undefined

Map v.s. Object

Map Object
key可以是 any value (object or primitive type) key 只能是 String 或者 Symbol
Map 有 size property Object 没有自带的 size property
Map 是一个 iterable, 所以它可以直接用 iterator 来迭代遍历 Object不是iterable, 遍历一个Object需要用一个方法去遍历它的key, 再通过key得到value
Map的key不会跟Map自带的property发生collide Object有prototype property, 可能添加的key会overwrite prototype里边的key。在ES5中可以用 map = Object.create(null) 来创建一个pure empty的object
当add/delete operation比较频繁的时候,Map的performance比较好

Map的Properties和methods

Properties
Map.prototype.size Returns the number of key/value pairs in the Map object.
Methods
Map.prototype.clear() Removes all key/value pairs from the Map object.
Map.prototype.delete(key) Returns true if an element in the Map object existed and has been removed, or false if the element does not exist. Map.prototype.has(key) will return false afterwards.
Map.prototype.entries() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
Map.prototype.forEach(callbackFn[, thisArg]) Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
Map.prototype.get(key) Returns the value associated to the key, or undefined if there is none.
Map.prototype.has(key) Returns a boolean asserting whether a value has been associated to the key in the Map object or not.
Map.prototype.keys() Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
Map.prototype.set(key, value) Sets the value for the key in the Map object. Returns the Map object.
Map.prototype.values() Returns a new Iterator object that contains the values for each element in the Map object in insertion order.
Map.prototype[@@iterator]() Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.

Useful Example

NaN as key

var myMap = new Map();
myMap.set(NaN, 'not a number');

myMap.get(NaN); // "not a number"

var otherNaN = Number('foo');
myMap.get(otherNaN); // "not a number"

Iterating Maps with for..of

var myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');
for (var [key, value] of myMap) {
  console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one

Relation with Array objects

var kvArray = [['key1', 'value1'], ['key2', 'value2']];

// Use the regular Map constructor to transform a 2D key-value Array into a map
var myMap = new Map(kvArray);

myMap.get('key1'); // returns "value1"

// Use the Array.from function to transform a map into a 2D key-value Array
console.log(Array.from(myMap)); // Will show you exactly the same Array as kvArray

// Or use the keys or values iterators and convert them to an array
console.log(Array.from(myMap.keys())); // Will show ["key1", "key2"]

WeakMap


Create a WeakMap

  • let weakmap = new WeakMap([iterable]);
  • iterable
    • An Array or other iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[1, 'one'], [2, 'two']]). Each key-value pair is added to the new Map; null values are treated as undefined

WeakMap v.s. Map

WeakMap Map
WeakMap的key只能是object type Map的key可以是任何type (object or primitive types)
WeakMap对key object的引用是弱引用,所以garbage collection机制可以将key object回收 Map对key object的引用是strong reference
WeakMap keys are not enumerable Map keys are enumerable. 所以如果要得到一个key 的list的话,应该考虑用Map

WeapMap的properties和methods

Properties
WeakMap.length The value of the length property is 0.
WeakMap.prototype.delete(key) Removes any value associated to the key. WeakMap.prototype.has(key) will return false afterwards.
WeakMap.prototype.get(key) Returns the value associated to the key, or undefined if there is none.
WeakMap.prototype.has(key) Returns a Boolean asserting whether a value has been associated to the key in the WeakMap object or not.
WeakMap.prototype.set(key, value) Sets the value for the key in the WeakMap object. Returns the WeakMap object.

MDN Documents

  • Map
  • WeakMap

Reference

JavaScript 内存泄漏教程

你可能感兴趣的:(ES6: Map, WeakMap)