Merge aria list

Benchmark created on


Setup

function usingSet(...ariaLists) {
  const dedupedAriaListSet = new Set()
  const output = []

  ariaLists.forEach(list => {
    list?.split(' ').forEach(value => {
      if (value) {
        if (!dedupedAriaListSet.has(value)) output.push(value)
        dedupedAriaListSet.add(value)
      }
    })
  })

  return output.length > 0 ? output.join(' ') : undefined
}

function usingArrayIncludes(...ariaLists) {
  const flattened = []
  ariaLists.forEach(value => {
    if (value) {
      value
        .trim()
        .split(' ')
        .forEach(val => {
          if (val !== '' && !flattened.includes(val)) flattened.push(val)
        })
    }
  })
  return flattened.length > 0 ? flattened.join(' ').trim() : undefined
}

function usingSetArrayFrom(...ariaLists) {
  const dedupedAriaListSet = new Set()

  ariaLists.forEach(list => {
    list?.split(' ').forEach(value => {
      if (value) dedupedAriaListSet.add(value)
    })
  })

  return dedupedAriaListSet.size > 0 ? Array.from(dedupedAriaListSet).join(' ') : undefined
}

Test runner

Ready to run.

Testing in
TestOps/sec
Using set
usingSet('do re mi fa', ' do re ', ' mi fa ')
ready
Using array.includes
usingArrayIncludes('do re mi fa', ' do re ', ' mi fa ')
ready
Using set & array.from
usingSetArrayFrom('do re mi fa', ' do re ', ' mi fa ')
ready

Revisions

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