Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create js Sets using arrays #339

Open
kwcantrell opened this issue Aug 18, 2020 · 1 comment
Open

Create js Sets using arrays #339

kwcantrell opened this issue Aug 18, 2020 · 1 comment
Assignees
Labels
performance refactoring Use best practices or improve the usage of public APIs

Comments

@kwcantrell
Copy link
Collaborator

The .add() function of Set is much slower than .push of an array. See this post for more details.

I also created simple test to verify and it seems creating an array and then converting it to a set is much faster than just using set.add(). In the below example, 'add by set' takes ~4 seconds will 'add by index' takes ~0.5 seconds. So, if we initialize all sets using arrays, we can save a significant amount of time. This would be especially helpful for the coloring/bar plot methods. Note: we still want to use sets because .has is much faster than indexOf.

var f = function() {
    var t = [];
    var d = new Date();
    for (var i = 0; i < 10000000; i ++) {
        t[i] = 1;
    }
    var st = new Set(t);
    st.has(1) ? console.log("!!!") : console.log(":(");
    var dt = new Date();
    console.log("add by index", dt.getTime() - d.getTime());

    var t = [];
    var d = new Date();
    for (var i = 0; i < 10000000; i ++) {
        t.push(i)
    }
    var st = new Set(t);
    st.has(1) ? console.log("!!!") : console.log(":(");
    var dt = new Date();
    console.log("add by push", dt.getTime() - d.getTime());

    var t = new Set();
    var d = new Date();
    for (var i = 0; i < 10000000; i ++) {
        t.add(i);
    }
    var dt = new Date();
    console.log("add by set", dt.getTime() - d.getTime());

    var d = new Date();
    var t = new Array(10000000);
    for (var i = 0; i < 10000000; i ++) {
        t[i] = 1;
    }
    var st = new Set(t);
    st.has(1) ? console.log("!!!") : console.log(":(");
    var dt = new Date();
    console.log("add by init", dt.getTime() - d.getTime());

    var d = new Date();
    var t = new Array(10000000).fill(0);
    for (var i = 0; i < 10000000; i ++) {
        t[i] = 1;
    }
    var st = new Set(t);
    st.has(1) ? console.log("!!!") : console.log(":(");
    var dt = new Date();
    console.log("add by init and fill", dt.getTime() - d.getTime());
}
f();
@kwcantrell kwcantrell added performance refactoring Use best practices or improve the usage of public APIs labels Aug 18, 2020
@kwcantrell kwcantrell self-assigned this Aug 18, 2020
@kwcantrell kwcantrell changed the title Create js using arrays Sets Create js Sets using arrays Aug 19, 2020
@kwcantrell kwcantrell mentioned this issue Aug 20, 2020
@kwcantrell
Copy link
Collaborator Author

kwcantrell commented Aug 26, 2020

We can actually remove Sets all together and use a boolean array in it place. According to this arrays only allocate memory to elements that actually hold a value. Thus, instead of adding a node to a set, we can simply use the nodes post-order position to set the corresponding array element to true. This would be beneficial for two reasons. 1) Creating an element in an array is much faster than adding an element to a Set. 2) We would no longer need to convert the Set back to an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance refactoring Use best practices or improve the usage of public APIs
Projects
None yet
Development

No branches or pull requests

1 participant