Last active
February 9, 2026 06:50
-
-
Save piroor/400cdd27696adcb131c87a05e40ac881 to your computer and use it in GitHub Desktop.
Benchmark of methods to determine two sets overlap or not in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // License: MIT | |
| // Original Author: YUKI "Piro" Hiroshi <yuki@clear-code.com> | |
| // confirmed at Firefox 147 | |
| async function test(times, size) { | |
| function compareAndForLoop(a, b) { | |
| const [smaller, larger] = a.size <= b.size ? [a, b] : [b, a]; | |
| for (const state of smaller) { | |
| if (larger.has(state)) | |
| return true; | |
| } | |
| return false; | |
| } | |
| function isDisjointFromOfSet(a, b) { | |
| return !a.isDisjointFrom(b); | |
| } | |
| function intersectionOfSet(a, b) { | |
| return a.intersection(b).size > 0; | |
| } | |
| function twoSets(a, b) { | |
| const unified = new Set([...a, ...b]); | |
| return unified.size < a.size + b.size; | |
| } | |
| function prepareArray(length) { | |
| const array = []; | |
| for (let i = 0; i < length; i++) { | |
| array.push(parseInt(Math.random() * (length / 10))); | |
| } | |
| return array; | |
| } | |
| const methods = [compareAndForLoop, isDisjointFromOfSet, intersectionOfSet, twoSets]; | |
| const results = []; | |
| for (const compare of methods) { | |
| const a = new Set(prepareArray(size)); | |
| const b = new Set(prepareArray(Math.ceil(size / 2))); | |
| const start = Date.now(); | |
| for (let i = 0; i < times; i++) { | |
| const result = compare(new Set(a), new Set(b)); | |
| } | |
| const delta = Date.now() - start; | |
| console.log(`${times}times, size=${size}, ${compare.name}`, delta); | |
| results.push(delta); | |
| await new Promise(resolve => setTimeout(resolve, 10)); | |
| } | |
| return results; | |
| } | |
| (async () => { | |
| const methods = ',compareAndForLoop,isDisjointFromOfSet,intersection,twoSets'; | |
| async function runForTimes(size, start, step) { | |
| const results = [methods]; | |
| for (let i = 0, maxi = 5; i < maxi; i++) { | |
| const times = start + (step * i); | |
| results.push(times + ',' + (await test(times, size)).join(',')); | |
| } | |
| console.log(results.join('\n')); | |
| } | |
| console.log('tests with small sets'); | |
| await runForTimes(10, 100000, 100000); | |
| console.log('tests with middle sets'); | |
| await runForTimes(300, 2500, 2500); | |
| console.log('tests with large sets'); | |
| await runForTimes(5000, 100, 100); | |
| async function runForLength(times, start, step) { | |
| const results = [methods]; | |
| for (let i = 0, maxi = 10; i < maxi; i++) { | |
| const size = start + (step * i); | |
| results.push(size + ',' + (await test(times, size)).join(',')); | |
| } | |
| console.log(results.join('\n')); | |
| } | |
| console.log('tests with increasing size'); | |
| await runForLength(5000, 100, 100); | |
| console.log('done'); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compareAndForLoop | isDisjointFromOfSet | intersection | twoSets | ||
|---|---|---|---|---|---|
| 100 | 3 | 4 | 7 | 16 | |
| 200 | 12 | 7 | 12 | 24 | |
| 300 | 10 | 11 | 25 | 42 | |
| 400 | 13 | 16 | 21 | 48 | |
| 500 | 20 | 26 | 36 | 63 | |
| 600 | 24 | 32 | 46 | 77 | |
| 700 | 28 | 35 | 66 | 98 | |
| 800 | 28 | 29 | 54 | 90 | |
| 900 | 51 | 55 | 74 | 105 | |
| 1000 | 41 | 38 | 80 | 108 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compareAndForLoop | isDisjointFromOfSet | intersection | twoSets | ||
|---|---|---|---|---|---|
| 100 | 7 | 8 | 10 | 18 | |
| 200 | 16 | 12 | 20 | 32 | |
| 300 | 20 | 19 | 31 | 54 | |
| 400 | 26 | 24 | 49 | 69 | |
| 500 | 30 | 34 | 56 | 74 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compareAndForLoop | isDisjointFromOfSet | intersection | twoSets | ||
|---|---|---|---|---|---|
| 2500 | 9 | 8 | 14 | 22 | |
| 5000 | 9 | 16 | 21 | 29 | |
| 7500 | 19 | 19 | 28 | 51 | |
| 10000 | 23 | 23 | 36 | 57 | |
| 12500 | 26 | 35 | 51 | 70 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compareAndForLoop | isDisjointFromOfSet | intersection | twoSets | ||
|---|---|---|---|---|---|
| 100000 | 69 | 54 | 79 | 107 | |
| 200000 | 91 | 103 | 129 | 161 | |
| 300000 | 138 | 144 | 155 | 249 | |
| 400000 | 175 | 188 | 202 | 296 | |
| 500000 | 230 | 228 | 229 | 354 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment