HTML Preparation code:
AخA
 
1
<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>
Script Preparation code:
x
 
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());
}
Tests:
  • Spreading

     
    const set3 = new Set([...set1, ...set2]);
    console.log(set3.size)
  • Set.forEach + Set.add

     
    const set3 = new Set(set1);
    set2.forEach(el => set3.add(el));
    console.log(set3.size)
  • Generators

     
    const set3 = new Set(function*() { yield* set1; yield* set2; }());
    console.log(set3.size)
  • for loop

     
    const set3 = new Set(set1);
    for(let el of set2) set3.add(el)
    console.log(set3.size)
  • builtin union

     
    console.log(set1.union(set2).size)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Spreading
    Set.forEach + Set.add
    Generators
    for loop
    builtin union

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 7 days ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
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