ECMAScript 6: what is WeakSet for?

后端 未结 7 716
北荒
北荒 2020-11-29 22:20

The WeakSet is supposed to store elements by weak reference. That is, if an object is not referenced by anything else, it should be cleaned from the WeakSet.

I have

7条回答
  •  心在旅途
    2020-11-29 22:41

    By definition, WeakSet has only three key functionalities

    • Weakly link an object into the set
    • Remove a link to an object from the set
    • Check if an object has already been linked to the set

    Sounds more pretty familiar?

    In some application, developers may need to implement a quick way to iterate through a series of data which is polluted by lots and lots of redundancy but you want to pick only ones which have not been processed before (unique). WeakSet could help you. See an example below:

    var processedBag = new WeakSet();
    var nextObject = getNext();
    while (nextObject !== null){
        // Check if already processed this similar object?
        if (!processedBag.has(nextObject)){
            // If not, process it and memorize 
            process(nextObject);
            processedBag.add(nextObject);
        }
        nextObject = getNext();
    }
    

    One of the best data structure for application above is Bloom filter which is very good for a massive data size. However, you can apply the use of WeakSet for this purpose as well.

提交回复
热议问题