Scotch Merge Arrays

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // 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
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Using sets
mergeArraysSet(['a','b','z'],['m','n','a'],['z','y'])
pending…
Using .filter()
mergeArraysFilter(['a','b','z'],['m','n','a'],['z','y'])
pending…
Using .reduce()
mergeArraysReduce(['a','b','z'],['m','n','a'],['z','y'])
pending…

Compare results of other browsers

Revisions

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

0 Comments