biocore / biocore/empress

Create js Sets using arrays

Open
#339 1 comment 1 reaction 1 assignee Claimed by @kwcantrell View on GitHub
performance refactoring
Dominant language
JavaScript
Stars
56
Forks
32
PR merge metrics
No merged PRs in 30d

Description

The .add() function of Set is much slower than .push of an array. See this [post](https://stackoverflow.com/questions/39007637/javascript-set-vs-array-performance) 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();
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.