BR CPF Validator

JavaScript performance comparison

Revision 2 of this test case created by Ken

Info

Testing different methods of validating a CPF

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var cpfToken0 = "544.561.110-89";
    var cpfToken1 = "761.177.510-36";
    var cpfToken2 = "181.764.172-70";
    var cpfToken3 = "281.832.434-36";
    var cpfToken4 = "430.329.202-80";
    var cpfToken5 = "111.111.111-11";
    var cpfToken6 = "111.111.111+11";
    var cpfToken7 = "111.111.111+111";
   
    var validateCPF1 = function(ssn) {
      // the below validation is very specific for CPF;
      // not for SSN in general
      var cpfToken = ssn;
      var base, checkDigits, sum, result;
   
      // Checks to see if the input has exactly 11 digits in it
      // The typical format is 11 straight digits or 3d.3d.3d-2d
      if (/^(\D*\d){11}$/.test(cpfToken)) {
        // Strips out any non-digit characters from the token
        cpfToken = cpfToken.replace(/\D/g, "");
        // ensure that there are at least 2 different digits
        if (/(\d)\1{10}/.test(cpfToken)) {
          return false;
        }
        base = cpfToken.substring(0, 11);
        checkDigits = cpfToken.substring(11);
        sum = 0;
        for (var i = 10; i > 1; i--) {
          sum += base.charAt(10 - i) * i;
        }
        result = sum % 11 < 2 ? 0 : 11 - sum % 11;
        // Checks to see if result equals the first checkDigits value
        if (result != checkDigits.charAt(0)) {
          return false;
        }
        base = cpfToken.substring(0, 10);
        sum = 0;
        for (var i = 11; i > 1; i--) {
          sum += base.charAt(11 - i) * i;
        }
        result = sum % 11 < 2 ? 0 : 11 - sum % 11;
        // Checks to see if result equals the second checkDigits value
        return result === checkDigits.charAt(1) - 0;
      }
      return false;
    };
   
    var validateCPF2 = function(cpfToken) {
      cpfToken = cpfToken.replace(/[\s\-\.]/g, "");
   
      if (/\D/g.test(cpfToken)) {
        return false;
      };
   
      if (cpfToken.length !== 11) {
        return false;
      }
   
      if (/(\d)\1{10}/.test(cpfToken)) {
        return false;
      }
   
      var digits = cpfToken.split(""),
        checkDigitIsValid = function(digitArray) {
          var numberOfDigits = digitArray.length,
            lastDigit = digitArray[numberOfDigits - 1] - 0,
            sum = 0,
            checkDigit, i;
   
          for (i = numberOfDigits; i > 1; i--) {
            sum += digitArray[numberOfDigits - i] * i;
          }
   
          checkDigit = sum % 11 < 2 ? 0 : 11 - sum % 11;
   
          return lastDigit === checkDigit;
        };
   
      if (checkDigitIsValid(digits.slice(0, 11)) === false) {
        return false;
      }
   
      if (checkDigitIsValid(digits.slice(0, 10)) === false) {
        return false;
      }
   
      return true;
    }
};
</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
Original
validateCPF1(cpfToken0);
validateCPF1(cpfToken1);
validateCPF1(cpfToken2);
validateCPF1(cpfToken3);
validateCPF1(cpfToken4);
validateCPF1(cpfToken5);
validateCPF1(cpfToken6);
validateCPF1(cpfToken7);
pending…
Rewrite
validateCPF1(cpfToken0);
validateCPF1(cpfToken1);
validateCPF1(cpfToken2);
validateCPF1(cpfToken3);
validateCPF1(cpfToken4);
validateCPF1(cpfToken5);
validateCPF1(cpfToken6);
validateCPF1(cpfToken7);
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