Uniqueness

Benchmark created by noahlaux on


Setup

var heroes = [
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false }
  ];
  
  var heroesLong = [
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false },
    { "name": "Rocco Siffredi", "age": 54, "long": true },
    { "name": "John Holmes", "age": 44, "long": true },
    { "name": "Peter North", "age": 61, "long": true },
    { "name": "Rocco Siffredi", "age": 54, "long": false }
  ];
  
  function uniqueOneliner(properties = []) {
    return (item, position, list) =>
      properties.some(
        property =>
          list.map(mapObj => mapObj[property]).indexOf(item[property]) ===
          position
      );
  }
  
  function unique(properties = []) {
    let valuePool;
  
    return (item, position, list) => {
      // cache valuePool
      if (!valuePool) {
        valuePool = properties.reduce((acc, property) => {
          acc[property] = list.map(mapObj => mapObj[property]);
          return acc;
        }, {});
      }
  
      const isDuplicate = Object.keys(valuePool).every(property => {
        const candidate = item[property];
        const foundIndex = valuePool[property].indexOf(candidate);
        return foundIndex !== position;
      });
  
      return !isDuplicate;
    };
  };

Test runner

Ready to run.

Testing in
TestOps/sec
oneliner short
heroes.filter(uniqueOneliner(['name', 'age']));
ready
cached short
heroes.filter(unique(['name', 'age']));
ready
oneliner long
heroesLong.filter(uniqueOneliner(['name', 'age']));
ready
cached long
heroesLong.filter(unique(['name', 'age']));
ready

Revisions

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

  • Revision 1: published by noahlaux on