Set, Map, WeakSet and WeakMap Details of SE6 New Features

Keywords: Javascript Java less Firefox

In SE5, we often use arrays or class array objects to manipulate data, but for some developers who are used to collections such as java, there is always a little less sense. SE6 provides Set s and Map s. It not only fundamentally provides solutions to some problems (such as data non-duplication), but also greatly improves performance.

To understand Set and Map, we need to start with its fundamental issues. Before going into a detailed API overview, I think it's necessary to make two points:

  • The order in which you traverse Set s and Map s is the order in which elements are inserted. This is the most different from other languages, because the author responsible for implementing ES6 collection module has been experimentally verified that sequential traversal in javaScript is more efficient than uncertain traversal.
  • Set and Map have no hash code. In other languages, it is possible to implement a hash function by itself and expose the default hash function of the system, but the javaScript Committee chose not to expose it.
On the second point, which may be somewhat difficult to understand, we illustrate by an example:
var a = {name:"Jhon"};
var b = {name:"Jhon"};
var set = new Set();
set.add(a).add(b);
console.log(set.size); //2
//These two objects should be treated in the same way, after all, they have exactly the same properties. But in JavaScript, they are independent and different from each other.

Read 10,000 volumes of books as well as operate them. I will try to validate these API s of the operation set one by one, and you will find that the official instructions may be different from the browser implementations.

Set set, no set of duplicate elements

var a = {name:"Jhon"};
var b = {name:"Jhon"};
var set = new Set(); // Create a new, empty Set
set.add(a).add(6); //Add two elements, add returns the collection itself, and can be called in a chain
console.log(set[0]); //undefined, index traversal is not supported
console.log(set.size); //Number of Print Elements 2
set.forEach(function(value){  //Traversing Set
	console.log(value); //Object { name: "Jhon" },6
});
if(set.has(a)){ //Determine whether an element exists
	console.log("a"); //a
}
if(set.has(b)){ //false
	console.log("b"); 
}
if(set.has({name:"Jhon"})){ //false
	console.log("other");
}
var c = [1,2,2,3,4];
var setc = new Set(c); //new Set(iterable): Extract elements from any traversable data and construct a new set.
console.log(setc.size); //4. Element de-weighting
console.log(setc.delete(4));//True, the official said delete would return the collection itself, but in the latest version of Firefox, if the element exists, it would return true, and vice versa, the chain call would report an error.
console.log(setc.size); //3
setc.clear(); //Empty Set
console.log(setc.size); //0

//set.keys(), set.values(), and set.entries() return various iterators that are provided for Map compatibility, so let's look at them later.


Map set, consisting of several key-value pairs

var map = new Map(); //Create a new, empty Map
map.set("one",1); //Add List Items
map.set("two",2);
console.log(map.size); //2. Get the number of elements
if(map.has("one")){ //true, Judging Element Existence
	console.log("one");
}
console.log(map.get("one")); //1. Get elements by key
map.forEach(function(value, key, map){ //Traversing map
	console.log(key + ":" + value); //one:1,two:2
});
for(var key of map.keys()){ //Iterator returning key
	console.log(key); //one,two
}
for(var value of map.values()){ //Iterator of Return Value
	console.log(value); //1,2
}
for(var [key,value] of map.entries()){ //Iterator of return item
	console.log(value); //1,2
}
console.log(map.delete("one")); //true, delete items according to key
map.clear(); //Empty map
console.log(map.size); //0

var a = [["one",1],["two",2]];
var map1 = new Map(a); //New Maps (pairs), creating Maps from arrays or existing maps
map1.forEach(function(value, key, map){ 
	console.log(key + ":" + value); //one:1,two:2
});


WeakSet and WeakMap weak reference sets

Both Map and Set maintain strong references to each key or value inside, that is, if an object is removed, the recovery mechanism cannot retrieve the memory it occupies unless it is deleted in Map or Set.

WeakSet does not maintain strong references to its objects. When an object in the WeakSet is reclaimed, it is simply removed from the WeakSet. WeakMap similarly does not maintain strong references to its keys. If a key is still used, the corresponding value is still used.
The following are some limitations of weak reference sets:
  • WeakMap supports only new, has, get, set, and delete.  
  • WeakSet supports only new, has, add, and delete.
  • The value of WeakSet and the key of WeakMap must be objects.
  • Neither can be iterated, unless you specifically query or give keys that interest you, you can't get items in a weak set.
Above is the operation of ES6 collection and precautions.





Posted by jipacek on Fri, 11 Jan 2019 09:48:10 -0800