age calc

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    ngetage3 = (function() {
      function formatstr(a, b, c) {
        b > 0 && a.push(b + c + (b > 1 ? "s" : "")) }
      function formatdate(years, months, days) {
        a = []
        formatstr(a, years , " year" );
        formatstr(a, months, " month");
        formatstr(a, days  , " day"  );
        a.length > 1 && (a[a.length - 1] = "and " + a[a.length - 1]);
        return(!a.length ? "0 days" : "" ) +
          a.join(a.length > 2 ? ", " : " ") + " old."
      }
      return function(a) {
        a = new Date(Date.parse(a.replace(/-/g, "/")));
        var b = new Date,
          years  = b.getYear () - a.getYear (),
          months = b.getMonth() - a.getMonth(),
          days   = b.getDate () - a.getDate ();
        if (b <= a) return "Invalid DOB."
        months < 0 && (years --, months +  12);
        days   < 0 && (months--, days   += 31);
        months < 0 && (years --, months  = 11);
        return formatdate(years, months, days);
      }
    })()
   
    ngetage2 = (function() {
      function formatstr(a, b, c) {
        b > 0 && a.push(b + c + (b > 1 ? "s" : "")) }
      return function(a) {
        a = new Date(Date.parse(a.replace(/-/g, "/")));
        var b = new Date,
          years  = b.getYear () - a.getYear (),
          months = b.getMonth() - a.getMonth(),
          days   = b.getDate () - a.getDate ();
        if (b <= a) return "Invalid DOB."
        months < 0 && (years --, months +  12);
        days   < 0 && (months--, days   += 31);
        months < 0 && (years --, months  = 11);
       
        a = []
        formatstr(a, years , " year" );
        formatstr(a, months, " month");
        formatstr(a, days  , " day"  );
        a.length > 1 && (a[a.length - 1] = "and " + a[a.length - 1]);
        return(!a.length ? "0 days" : "" ) +
          a.join(a.length > 2 ? ", " : " ") + " old."
      }
    })()
   
    function ngetage(a) {
      a = new Date(Date.parse(a.replace(/-/g, "/")));
      var b = new Date,
          years = b.getYear() - a.getYear(),
          months = b.getMonth() - a.getMonth(),
          days = b.getDate() - a.getDate();
      if (b <= a) return "Invalid DOB."
      months < 0 && (years--, months + 12);
      days < 0 && (months--, days += 31);
      months < 0 && (years--, months = 11);
   
      a = [], b = function(b, c) {
        b > 0 && a.push(b + c + (b > 1 ? "s" : ""))
      }
      b(years, " year");
      b(months, " month");
      b(days, " day");
      a.length > 1 && (a[a.length - 1] = "and " + a[a.length - 1]);
      return (!a.length ? "0 days" : "") + a.join(a.length > 2 ? ", " : " ") + " old."
    }
   
    function ogetage(dateString) {
      var now = new Date();
      var today = new Date(now.getYear(), now.getMonth(), now.getDate());
      var yearNow = now.getYear();
      var monthNow = now.getMonth();
      var dateNow = now.getDate();
      var dob = new Date(dateString.substring(0, 4), dateString.substring(5, 7) - 1, dateString.substring(8, 11));
      var yearDob = dob.getYear();
      var monthDob = dob.getMonth();
      var dateDob = dob.getDate();
      var age = {};
      var ageString = "";
      var yearString = "";
      var monthString = "";
      var dayString = "";
      yearAge = yearNow - yearDob;
      if (monthNow >= monthDob) var monthAge = monthNow - monthDob;
      else {
        yearAge--;
        var monthAge = 12 + monthNow - monthDob;
      }
      if (dateNow >= dateDob) var dateAge = dateNow - dateDob;
      else {
        monthAge--;
        var dateAge = 31 + dateNow - dateDob;
        if (monthAge < 0) {
          monthAge = 11;
          yearAge--;
        }
      }
      age = {
        years: yearAge,
        months: monthAge,
        days: dateAge
      };
      if (age.years > 1) yearString = " years";
      else yearString = " year";
      if (age.months > 1) monthString = " months";
      else monthString = " month";
      if (age.days > 1) dayString = " days";
      else dayString = " day";
      if ((age.years > 0) && (age.months > 0) && (age.days > 0)) ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old.";
      else if ((age.years == 0) && (age.months == 0) && (age.days > 0)) ageString = "Only " + age.days + dayString + " old!";
      else if ((age.years > 0) && (age.months == 0) && (age.days == 0)) ageString = age.years + yearString + " old. Happy Birthday!!";
      else if ((age.years > 0) && (age.months > 0) && (age.days == 0)) ageString = age.years + yearString + " and " + age.months + monthString + " old.";
      else if ((age.years == 0) && (age.months > 0) && (age.days > 0)) ageString = age.months + monthString + " and " + age.days + dayString + " old.";
      else if ((age.years > 0) && (age.months == 0) && (age.days > 0)) ageString = age.years + yearString + " and " + age.days + dayString + " old.";
      else if ((age.years == 0) && (age.months > 0) && (age.days == 0)) ageString = age.months + monthString + " old.";
      else ageString = "Oops! Could not calculate age!";
      return ageString;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Old age calculator
ogetage("1994-10-25");
ogetage("2012-12-14");
ogetage("2001-12-28");
ogetage("1995-02-28");
ogetage("2012-02-26");
ogetage("2008-12-23");
ogetage("2012-07-28");
ogetage("2012-12-28");
pending…
New age calculator
ngetage("1994-10-25"); // 18 years, 2 months, and 3 days old.
ngetage("2012-12-14"); // 14 days old.
ngetage("2001-12-28"); // 11 years old.
ngetage("1995-02-28"); // 17 years and 10 months old.
ngetage("2012-02-26"); // 10 months and 2 days old.
ngetage("2008-12-23"); // 4 years and 5 days old.
ngetage("2012-07-28"); // 5 months old.
ngetage("2012-12-28"); // 0 days old.
pending…
New age calculator 2
ngetage2("1994-10-25"); // 18 years, 2 months, and 3 days old.
ngetage2("2012-12-14"); // 14 days old.
ngetage2("2001-12-28"); // 11 years old.
ngetage2("1995-02-28"); // 17 years and 10 months old.
ngetage2("2012-02-26"); // 10 months and 2 days old.
ngetage2("2008-12-23"); // 4 years and 5 days old.
ngetage2("2012-07-28"); // 5 months old.
ngetage2("2012-12-28"); // 0 days old.
pending…
New age calculator 3
ngetage3("1994-10-25"); // 18 years, 2 months, and 3 days old.
ngetage3("2012-12-14"); // 14 days old.
ngetage3("2001-12-28"); // 11 years old.
ngetage3("1995-02-28"); // 17 years and 10 months old.
ngetage3("2012-02-26"); // 10 months and 2 days old.
ngetage3("2008-12-23"); // 4 years and 5 days old.
ngetage3("2012-07-28"); // 5 months old.
ngetage3("2012-12-28"); // 0 days old.
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. Here’s a list of current revisions for this page:

0 comments

Add a comment