BR CPF Validator

JavaScript performance comparison

Test case created by Ken

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var cpfToken0 = "544.561.110-89";
  var cpfToken1 = "761.177.510-36";
  var cpfToken2 = "886.390.535-53";
  var cpfToken3 = "281.832.434-36";
  var cpfToken4 = "430.329.202-80";
  
  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) {
      // Strip out any non-digit characters from the token
      cpfToken = cpfToken.replace(/\D/g,"");
  
      // ensure the CPF Token has to has 11 digits.
      if(cpfToken.length !== 11){
          return false;
      }
  
      // ensure that there are at least 2 different digits
      if (/(\d)\1{10}/.test(cpfToken)) {
          return false;
      }
  
      var digits = cpfToken.split(),
          checkDigitIsValid = function(digitArray){
              // Checks if the last digit is the correct digit
              // based on the check digit algorithm
              var numberOfDigits = digitArray.length,
                  lastDigit = digitArray[numberOfDigits - 1],
                  sum, checkDigit, i;
  
              sum = 0;
              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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Original
validateCPF1(cpfToken0);
validateCPF1(cpfToken1);
validateCPF1(cpfToken2);
validateCPF1(cpfToken3);
validateCPF1(cpfToken4);
pending…
Rewrite
validateCPF2(cpfToken0);
validateCPF2(cpfToken1);
validateCPF2(cpfToken2);
validateCPF2(cpfToken3);
validateCPF2(cpfToken4);
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.

0 Comments