Unique by key

JavaScript performance comparison

Test case created by Aidurber

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var data = [];
  for(let i = 0; i < 10000; i++){
   	data.push({id: i % 2 === 0 ? 1 : 2});
  }
  

};

Benchmark.prototype.teardown = function() {
  data = [];

};
</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
Filter map
function uniqueByMap(myArr, prop) {
   return myArr.filter((obj, pos, arr) => {
     return arr.map((mapObj) => mapObj[prop]).indexOf(obj[prop]) === pos;
   });
  }

uniqueByMap(data, 'id');
pending…
Filter single
function uniqueBy(arr, prop) {
     return arr.filter((record, index, self) => {
       return index === self.findIndex((t) => t[prop] === record[prop]);
     });
  }

uniqueBy(data, 'id');
pending…
Without predicate
function uniqueByNoPredicate(arr, prop){
  const len = arr.length
  const data = []
  const seen = []
  for (let i = 0; i < len; i++) {
    const item = arr[i]
    const val = item[prop]
    if (!~seen.indexOf(val)) {
      seen.push(val)
      data.push(arr[i])
    }
  }

  return data
}

uniqueByNoPredicate(data, 'id')
pending…
Without predicate with set
function uniqueWithSet(arr, prop){
  const len = arr.length
  const data = []
  const seen = new Set()
  for (let i = 0; i < len; i++) {
    const item = arr[i]
    const val = item[prop]
    if (!seen.has(val)) {
      seen.add(val)
      data.push(arr[i])
    }
  }

  return data
}

uniqueWithSet(data, 'id')
pending…
While no predicate
function uniqueByWhile(arr, prop){
    const len = arr.length
  let cursor = 0
  const record = []
  const seen = []
  while (cursor < len) {
    const item = arr[cursor]
    const val = item[prop]
    if (!~seen.indexOf(val)) {
      seen.push(val)
      record.push(item)
    }
    cursor++
  }
  return record
}

uniqueByWhile(data, 'id')
pending…
Map revisit
function uniqueByMap(myArr, prop) {
   const vals = myArr.map(obj => obj[prop]);
   return myArr.filter((obj, pos, arr) => {
     return vals.indexOf(obj[prop]) === pos;
   });
  }

uniqueByMap(data, 'id');
pending…
While decrement
function uniqueByDecrement(arr, prop){
  let len = arr.length
  const record = []
  const seen = []
  while (len--) {
    const item = arr[len]
    const val = item[prop]
    if (!~seen.indexOf(val)) {
      seen.push(val)
      record.push(item)
    }
  }
  return record.reverse()
}

uniqueByDecrement(data, 'id')
pending…
While decrement forget order
function uniqueByDecrement(arr, prop){
  let len = arr.length
  const record = []
  const seen = []
  while (len--) {
    const item = arr[len]
    const val = item[prop]
    if (!~seen.indexOf(val)) {
      seen.push(val)
      record.push(item)
    }
  }
  return record
}

uniqueByDecrement(data, 'id')
pending…
While increment v2
function uniqueBy(arr, prop){
  const len = arr.length
  let cursor = -1
  const record = []
  const seen = []
  while (++cursor < len) {
    if (!~seen.indexOf(arr[cursor][prop])) {
      seen.push(arr[cursor][prop])
      record.push(arr[cursor])
    }
  }
  return record
}

uniqueBy(data, 'id')
pending…
While increment with reduced property access
function uniqueBy(arr, prop){
  const len = arr.length
  let cursor = -1
  const record = []
  const seen = []
  while (++cursor < len) {
const item = arr[cursor];
const val = item[prop];
    if (!~seen.indexOf(val)) {
      seen.push(val)
      record.push(item)
    }
  }
  return record
}

uniqueBy(data, 'id')
pending…

Revisions

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

0 Comments