Dedup of arrays containing objects

JavaScript performance comparison

Test case created by Chris Andrejewski

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const a = {foo: 1}
  const b = {bar: 2}
  const c = {baz: 3}
  const a2 = a
  const a3 = a
  const b2 = b
  const testArray = [a, b, c, a2, b2, a3]
  
  function functionalDedup (array) {
  	return array.reduce((uniques, item) => {
    	return uniques.includes(item) ? uniques : uniques.concat(item)
    }, [])
  }
  
  function proceduralDedup (array) {
  	const len = array.length; // saves a lookup if it is not optimized
    if (len === 0) return array
    const uniques = [array[0]]
  	for (let index = 1; index < len; index++) {
    	let item = array[index]
    	let isUnique = true // until proven guilty
      let checkIndex = index
      while (checkIndex) {
      	if (item === array[--checkIndex]) {
        	isUnique = false
        	break;
        }
      }
      if (isUnique) {
      	uniques.push(item)
      }
    }
    return uniques
  }

};
</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
functional
functionalDedup(testArray)
pending…
procedural
proceduralDedup(testArray)
pending…

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

0 Comments