isLeapYear

JavaScript performance comparison

Test case created by jesusvilla

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function checkYears(fn) {
      const years = [1900, 1970, 1991, 1995, 2000, 2007, 2010];
      for (let i = 0, length = years.length; i < length; i++) {
        fn(i);
      }
    }
  
  const REGEX_LEAP = /([48cg]|[^5af]0)$/;

};
</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
MOD (equals zero)
function isLeapYear(year) {
  return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
}

checkYears(isLeapYear);
pending…
MOD (truthy)
function isLeapYear(year) {
  return !(year % 4) && !!(year % 100) || !(year % 400);
}

checkYears(isLeapYear);
pending…
Check 29º February
function isLeapYear(year) {
  return new Date(year, 1, 29).getDate() === 29;
}

checkYears(isLeapYear);
pending…
Regex: Base20
function isLeapYear(year) {
  return REGEX_LEAP.test(year.toString(20));
}

checkYears(isLeapYear);
pending…
Odd bit manipulations
function isLeapYear(year) {
  return !(year & 3 || year & 15 && !(year % 25));
}

checkYears(isLeapYear);
pending…
MOD and XOR
function isLeapYear(year) {
  return ((year % 4 === 0) ^ (year % 100 === 0) ^ (year % 400 === 0)) === 1;
}

checkYears(isLeapYear);
pending…

Revisions

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

0 Comments