Scotch Merge Arrays (v9)

Revision 9 of this benchmark created by Volker Nauruhn on


Setup

// USING A SET
  function mergeArraysSet(...arrays) {
  
      let jointArray = []
      
      arrays.forEach(array => {
          jointArray = [...jointArray, ...array]
      });
  
      return [...new Set([...jointArray])]
  
  }
  
  // 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
  }
  
  		function combiningArrays(...arrays) {
  			const resultArray = [];
  
  			arrays.forEach(array => {
  				array.forEach(element => {
  					if (resultArray.indexOf(element) === -1) {
  						resultArray.push(element);
  					}
  				})
  			})
  
  			return resultArray;
  		}
  
  		function combiningArraysTwo(...arrays) {
  			const resultArray = [];
  
  			arrays.forEach(array => {
  				array.forEach(element => {
  					if (!resultArray.includes(element)) {
  						resultArray.push(element);
  					}
  				})
  			})
  
  			return resultArray;
  		}

Test runner

Ready to run.

Testing in
TestOps/sec
Using iterations
combiningArrays(['a','b','z'],['m','n','a'],['z','y'])
ready
Using iterations with contains
combiningArraysTwo(['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
Using .filter()
mergeArraysFilter(['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