Scotch Merge Arrays (v3)

Revision 3 of this benchmark created by Cătălin Tănăsescu on


Setup

// USING A SET
  function mergeArraysSet(...arrays) {
  
      
      return Array.from(new Set([].concat(...arrays)))
  
  }
  
  // USING .FILTER()
  
  function mergeArraysFilter(...arrays) {
  
      let jointArray = []
      
      arrays.forEach(array => {
          jointArray = [...jointArray, ...array]
      })
  
      const uniqueArray = jointArray.filter((item,index) => jointArray.indexOf(item) === index)
  
      return uniqueArray
  }
  
  // USING .REDUCE()
  
  function mergeArraysReduce(...arrays) {
  
      let jointArray = []
      
      arrays.forEach(array => {
          jointArray = [...jointArray, ...array]
      })
  
      const uniqueArray = jointArray.reduce((newArray, item) =>{
          if (newArray.includes(item)){
              return newArray
          } else {
              return [...newArray, item]
          }
      }, [])
  
      return uniqueArray
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Using .filter()
mergeArraysFilter(['a','b','z'],['m','n','a'],['z','y'])
ready
Using .reduce()
mergeArraysReduce(['a','b','z'],['m','n','a'],['z','y'])
ready
Using sets
mergeArraysSet(['a','b','z'],['m','n','a'],['z','y'])
ready

Revisions

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

  • Revision 3: published by Cătălin Tănăsescu on
  • Revision 6: published by Jayce Crowther on
  • Revision 9: published by Volker Nauruhn on