Test name | Executions per second |
---|---|
Spreading | 459560.8 Ops/sec |
Set.forEach + Set.add | 466256.5 Ops/sec |
Generators | 406054.9 Ops/sec |
for loop | 445394.8 Ops/sec |
builtin union | 453611.9 Ops/sec |
<script src="https://cdnjs.cloudflare.com/ajax/libs/nanoid/4.0.1/index.browser.js" integrity="sha512-YFaQHp+hWX9CMeIMngYK23kSIWaYlgsswmzmIdEw/HcK/5NLhXY2MbT0wQB5DnUzjW1uky4quIHtksukqZGkMw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
const SET_SIZE = 3000;
var set1 = new Set()
var set2 = new Set()
while (i < SET_SIZE) {
const nid = 'key:'+Math.random();
set1.add(nid);
if (i % 2 == 0) set2.add(nid);
i++;
}
while (i < SET_SIZE / 2) {
set2.add('key:'+Math.random());
}
const set3 = new Set([set1, set2]);
console.log(set3.size)
const set3 = new Set(set1);
set2.forEach(el => set3.add(el));
console.log(set3.size)
const set3 = new Set(function*() { yield* set1; yield* set2; }());
console.log(set3.size)
const set3 = new Set(set1);
for(let el of set2) set3.add(el)
console.log(set3.size)
console.log(set1.union(set2).size)