Set Theory (v2)

Revision 2 publishedon

Description

Compare perf of allocating and comparing sets, vs. arrays

Setup

const arrA = [1, 2, 3]
const arrB = [2, 3, 4]

const arr1000A = Array.from({ length: 1000 }).map((_, i) => i)
const arr1000B = [...arr1000A.slice(1), 1000]

const arr10000A = Array.from({ length: 10000 }).map((_, i) => i)
const arr10000B = [...arr10000A.slice(1), 10000]

const arr100000A = Array.from({ length: 100000 }).map((_, i) => i)
const arr100000B = [...arr100000A.slice(1), 100000]

const arr1000000A = Array.from({ length: 1000000 }).map((_, i) => i)
const arr1000000B = [...arr1000000A.slice(1), 1000000]

Test Runner

Initializing...

Testing in
Test CaseOps/sec
Set.prototype.has
const a = new Set(arrA)
const b = new Set(arrB)
const hasChanged =
  [...a].some(item => !b.has(item)) ||
  [...b].some(item => !a.has(item))
ready
Array.prototype.includes
const hasChanged =
  arrA.some(item => !arrB.includes(item)) ||
  arrB.some(item => !arrA.includes(item))
ready
Set.prototype.difference
const a = new Set(arrA)
const b = new Set(arrB)
const hasChanged =
  !!a.symmetricDifference(b).size
ready
Set.prototype.has - 1000 elements
const a = new Set(arr1000A)
const b = new Set(arr1000B)
const hasChanged =
  [...a].some(item => !b.has(item)) ||
  [...b].some(item => !a.has(item))
ready
Array.prototype.includes - 1000 elements
const a = arr1000A
const b = arr1000B
const hasChanged =
  a.some(item => !b.includes(item)) ||
  b.some(item => !a.includes(item))
ready
Set.prototype.has - 10,000 elements
const a = new Set(arr10000A)
const b = new Set(arr10000B)
const hasChanged =
  [...a].some(item => !b.has(item)) ||
  [...b].some(item => !a.has(item))
ready
Array.prototype.includes - 10,000 elements
const a = arr10000A
const b = arr10000B
const hasChanged =
  a.some(item => !b.includes(item)) ||
  b.some(item => !a.includes(item))
ready
Set.prototype.has - 100,000 elements
const a = new Set(arr100000A)
const b = new Set(arr100000B)
const hasChanged =
  [...a].some(item => !b.has(item)) ||
  [...b].some(item => !a.has(item))
ready
Array.prototype.includes - 100,000 elements
const a = arr100000A
const b = arr100000B
const hasChanged =
  a.some(item => !b.includes(item)) ||
  b.some(item => !a.includes(item))
ready
Set.prototype.has - 1,000,000 elements
const a = new Set(arr1000000A)
const b = new Set(arr1000000B)
const hasChanged =
  [...a].some(item => !b.has(item)) ||
  [...b].some(item => !a.has(item))
ready
Array.prototype.includes - 1,000,000 elements
const a = arr1000000A
const b = arr1000000B
const hasChanged =
  a.some(item => !b.includes(item)) ||
  b.some(item => !a.includes(item))
ready
Set.prototype.difference - 1000 elements
const a = new Set(arr1000A)
const b = new Set(arr1000B)
const hasChanged =
  !!a.symmetricDifference(b).size
ready
Set.prototype.difference - 10,000 elements
const a = new Set(arr10000A)
const b = new Set(arr10000B)
const hasChanged =
  !!a.symmetricDifference(b).size
ready
Set.prototype.difference - 100,000 elements
const a = new Set(arr100000A)
const b = new Set(arr100000B)
const hasChanged =
  !!a.symmetricDifference(b).size
ready
Set.prototype.difference - 1,000,000 elements
const a = new Set(arr1000000A)
const b = new Set(arr1000000B)
const hasChanged =
  !!a.symmetricDifference(b).size
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

Revision 1
publishedon
Revision 2
publishedon