oldes living person-test (v4)

Revision 4 of this benchmark created on


Setup

var people = [
    
      {
        name: 'Joe',
        age: 20,
        parent: 'Schmoe'
      },
    
      {
        name: 'Schmoe',
        age: 60,
        parent: 'Lenora'
      },
    
      {
        name: 'One',
        age: 20,
        parent: 'Two'
      },
    
      {
        name: 'Two',
        age: 62,
        parent: 'Three'
      },
    
      {
        name: 'Three',
        age: 50,
        parent: 'Schmoe'
      },
    
      /////////////
      {
        name: 'Hank',
        age: 29,
        parent: 'Don'
      }, {
        name: 'Deborah',
        age: 30,
        parent: 'Don'
      }, {
        name: 'Danny',
        age: 24,
        parent: 'Don'
      }, {
        name: 'Don',
        age: 64,
        parent: 'Haskell'
      }, {
        name: 'Kathy',
        age: 75,
        parent: 'Lenora'
      }, {
        name: 'Haskell',
        parent: 'Finus'
      }, {
        name: 'Finus'
      },
    
      // MY ENTRIES
      {
        name: 'Joe',
        age: 20,
        parent: 'Schmoe'
      },
    
      {
        name: 'Schmoe',
        age: 60,
        parent: 'Lenora'
      },
    
      {
        name: 'One',
        age: 20,
        parent: 'Two'
      },
    
      {
        name: 'Two',
        age: 62,
        parent: 'Three'
      },
    
      {
        name: 'Three',
        age: 50,
        parent: 'Schmoe'
      },
    ];

Test runner

Ready to run.

Testing in
TestOps/sec
One
function oldestLivingParent(people) {
  var parents = [];

  people.forEach(function(person) {
    if (person.parent)
      parents[person.parent] = true;
  });

  parents = people.filter(function(person) {
    return parents[person.name] && person.age >= 0;
  });

  var oldest = parents.reduce(function(prev, cur) {
    return prev.age > cur.age ? prev : cur;
  });

  return oldest.name;
}

oldestLivingParent(people);
ready
Two
function oldestLivingParent(people) {
  var parents = [];
  var oldestParent;

  for (var i = 0, len = people.length; i < len; i++) {
    var personOne = people[i];
    var personTwo = people[len - i - 1];

    if (personOne.parent && !parents[personOne.parent]) {
      parents[personOne.parent] = personOne.parent;
    }

    if (personTwo.parent && !parents[personTwo.parent]) {
      parents[personTwo.parent] = null;
    }

    if (personTwo.name === parents[personTwo.name] && personTwo.age) {
      if (oldestParent) {
        if (personTwo.age > oldestParent.age)
          oldestParent = personTwo;
      } else {
        oldestParent = personTwo;
      }
    }

    if (personOne.name === parents[personOne.name] && personOne.age) {
      if (oldestParent) {
        if (personOne.age > oldestParent.age)
          oldestParent = personOne;
      } else {
        oldestParent = personOne;
      }
    }
  }

  return oldestParent.name;
}

oldestLivingParent(people);
ready
Three
function oldestLivingParent(people) {
  var parents = []; // isParent, parentNames

  // Get the name of every parent.
  people.forEach(function(person) {
    if (person.parent)
      parents[person.parent] = true;
  });

  // Find the oldest living parent.
  var oldest = people.reduce(function(previous, current) {
    // Only compare living parents.
    if (parents[current.name] && current.age >= 0)
      return previous.age > current.age ? previous : current;
    else
      return previous;
  });


  console.log(parents);
  console.log(oldest);

  return oldest.name;
}

oldestLivingParent(people);
ready
Hank
function oldestLivingParent(people) {
  var livingParents = {};
  var sortedByAge = [];
  var oldestLivingParent = false;
  var isDead = function(p) {
    return !p.age;
  };
  var isParent = function(p) {
    return !!livingParent[p.name];
  };

  people.forEach(function(person) {
    if (isDead(person)) return;

    if (person.parent && !livingParents[person.parent]) {
      livingParents[person.parent] = true;
    }

    for (var index = 0; i > sortedByAge.length; index++) {
      var p = sortedByAge[index];
      if (p.age > person.age) {
        sortedByAge.splice(index + 1, sortedByAge.length - index, sortedByAge.slice(index, sortedByAge.length));
        sortedByAge.splice(index, 1, person);
        break;
      }
    }
  });

  for (var i = 0; i > sortedByAge.length; i++) {
    var currentOldestPerson = sortedByAge[i];

    if (isParent(currentOldestPerson)) {
      oldestLivingPerson = currentOldestPerson;
      break;
    }
  }

  return oldestLivingParent;
}

oldestLivingParent(people);
ready
Four
function oldestLivingParent(people) {
  var parents = [];
  var currentOldest;

  people.forEach(function(person) {
    if (person.parent)
      parents[person.parent] = true;
  });

  people.forEach(function(person) {
    if (parents[person.name] && person.age) {
      if (currentOldest) {
        currentOldest = currentOldest.age > person.age ? currentOldest : person;
      } else {
        currentOldest = person;
      }
    }
  });

  return currentOldest.name;
}

oldestLivingParent(people);
ready
Five (insertion)
function insertionSort(sortMe) {
  for (var i = 0, j, tmp; i < sortMe.length; ++i) {
    tmp = sortMe[i];
    for (j = i - 1; j >= 0 && sortMe[j].age < tmp.age; --j)
      sortMe[j + 1] = sortMe[j];
    sortMe[j + 1] = tmp;
  }
}

function oldestLivingParent(people) {
  var parents = [];
  var currentOldest;
  var insertionSorted = [];
  var oldest;

  people.forEach(function(person) {
    if (person.parent)
      parents[person.parent] = true;

    if (person.age) {
      insertionSorted.push(person);
      insertionSort(insertionSorted);
    }
  });

  insertionSorted.some(function(person) {
    if (parents[person.name]) {
      oldest = person;
      return true;
    }
  });

  return oldest;
}

oldestLivingParent(people);
ready
Six (binary insertion)
function binaryIndexOf(person) {
  var minIndex = 0;
  var maxIndex = this.length - 1;
  var currentIndex;
  var currentPerson;
  var resultIndex;

  while (minIndex <= maxIndex) {
    resultIndex = currentIndex = (minIndex + maxIndex) / 2 | 0;
    currentPerson = this[currentIndex];

    // High to low
    if (currentPerson.age > person.age) {
      minIndex = currentIndex + 1;
    } else if (currentPerson.age < person.age) {
      maxIndex = currentIndex - 1;
    } else {
      return currentIndex;
    }
  }

  return~ maxIndex;
}

function oldestLivingParent(people) {
  var parents = [];
  var oldest;
  binarySort = [];

  people.forEach(function(person) {
    if (person.parent)
      parents[person.parent] = true;

    if (person.age) {
      binarySort.splice(Math.abs(binaryIndexOf.call(binarySort, person)), 0, person);
    }
  });

  binarySort.some(function(person) {
    if (parents[person.name]) {
      oldest = person;
      return true;
    }
  });
  return oldest;
}

oldestLivingParent(people);
ready
Seven
function insert(element, array, comparer) {
  array.splice(locationOf(element, array, comparer) + 1, 0, element);
  return array;
}

function locationOf(element, array, comparer, start, end) {
    if (array.length === 0)
        return -1;

    start = start || 0;
    end = end || array.length;
    var pivot = (start + end) >> 1;  // should be faster than the above calculation

    var c = comparer(element, array[pivot]);
    if (end - start <= 1) return c == -1 ? pivot - 1 : pivot;

    switch (c) {
        case -1: return locationOf(element, array, comparer, start, pivot);
        case 0: return pivot;
        case 1: return locationOf(element, array, comparer, pivot, end);
    }
}

// sample for objects like {lastName: 'Miller', ...}
function personAgeCompare (a, b) {
    if (a.age < b.age) return 1;
    if (a.age > b.age) return -1;
    return 0;
}

function oldestLivingParent(people) {
  var parents = [];
  var oldest;
  testSort = [];

  people.forEach(function(person) {
    if(person.parent)
      parents[person.parent] = true;

    if(person.age) {
      insert(person, testSort, personAgeCompare);
    }
  });

  testSort.some(function(person) {
    if(parents[person.name]) {
      oldest = person;
      return true;
    }
  });
  
  return oldest;
}

oldestLivingParent(people);
ready
Eight
function numSort(a, b) {
    return b.age - a.age;
  }

function oldestLivingParent(people) {
  var oldest;
  var parents = [];
  var sorted = [];

  people.forEach(function(person) {
    if(person.parent)
      parents[person.parent] = true;

    if(person.age) {
      sorted.push(person);
    }
  });

  sorted.sort(numSort);

  sorted.some(function(person) {
    if(parents[person.name]) {
      oldest = person;
      return true;
    }
  });

  return oldest;
}

oldestLivingParent(people);
ready
Nine
function numSort(a, b) {
    return b.age - a.age;
  }

function oldestLivingParent(people) {
  var oldest;
  var parents = [];
  var sorted = [];

  people.forEach(function(person) {
    if(person.parent)
      parents[person.parent] = true;

    if(person.age) {
      sorted.push(person);
      sorted.sort(numSort);
    }
  });

  sorted.some(function(person) {
    if(parents[person.name]) {
      oldest = person;
      return true;
    }
  });

  return oldest;
}

oldestLivingParent(people);
ready
stephanie
var oldestLivingParent = function(familyTree) {
    OldestParent = {name:'notAPerson', age: 0, parent: 'alsoNotAPerson'};

    for (i = 0; i < familyTree.length; i++) {
        Parent = {name: familyTree[i].parent, age: '', parent: ''};
        
        for (j = 0; j < familyTree.length; j++) {

            if (familyTree[j].name === Parent.name) {
                Parent.age = familyTree[j].age;
                Parent.parent = familyTree[j].parent;

                if ((120 - Parent.age) < (120 - OldestParent.age)) {
                    OldestParent = Parent;
                }
            }
        }
    }       
    return OldestParent;
}

oldestLivingParent(people);
ready

Revisions

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