QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Difference between Set and WeakSet in JavaScript

Difference between Set and WeakSet in JavaScript

This post is a part 14 of ECMAScript 6 Complete Tutorial post series.

JavaScript Set and WeakSet objects allows you to store collection of unique keys. But there are some key differences between them. Here are the differences:

  • They both behave differently when a object referenced by their keys gets deleted. Lets take the below example code:

    var set = new Set();
    var weakset = new WeakSet();

    (function(){
        var a = {x: 12};
        var b = {y: 12};

        set.add(a);
        weakset.add(b);
    })()

    One the above self invoked function is executed there is no way we can reference {x: 12} and {y: 12} anymore. Garbage collector goes ahead and deletes the key b pointer from “WeakSet” and also removes {y: 12} from memory. But in case of “Set”, the garbage collector doesn’t remove a pointer from “Set” and also doesn’t remove {x: 12} from memory.

    So “Set” can cause more garbages in memory. We can say that “Set” references are strong pointer whereas “WeakSet” references are weak pointers.

  • “WeakSet” keys cannot be primitive types. Nor they can be created by an array or another set.

    var set = new Set([1,2,3,4]);

    //cannot be created from array or another set
    var weakset = new WeakSet();
    weakset.add({a: 1}); //object reference must
  • “WeakSet” doesn’t provide any methods or functions to work with the whole set of keys. For example: size, looping etc.

    var set = new Set([1,2,3,4]);

    //cannot be created from array
    var weakset = new WeakSet();
    weakset.add({a: 1}); //object reference must

    console.log(set.size);//4
    console.log(weakset.size);//undefined

    for(var i of set)
    {
        console.log(i); //1,2.3.4
    }

    //doesn't execute throws error
    for(var i of weakset)
    {
        console.log(i);
    }

    set.clear();
    weakset.clear(); //This works
Feb 19, 2015Narayan Prusty
JavaScript "Set" ObjectJavaScript "Map" Object
Comments: 2
  1. Valtteri
    6 years ago

    This is because WeakSet doesn’t have clear() anymore: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet#Methods

    ReplyCancel
  2. rajasekar
    6 years ago

    when i run this code ….error accurs weakset.clear is not a function

    ReplyCancel

Leave a Reply to rajasekar Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago 2 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • Difference between “Map” and “WeakMap” in JavaScript
  • JavaScript Global Variables And Memory Leakage
  • JavaScript “Map” Object
  • JavaScript “Set” Object
  • JavaScript ArrayBuffers And Typed Arrays
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license