Tests:
  • FCC

    x
     
    // Solution with Regular expression and Array of ASCII character codes
    function rot13(str) {
      var rotCharArray = [];
      var regEx = /[A-Z]/ ;
      str = str.split("");
      for (var x in str) {
        if (regEx.test(str[x])) {
          // A more general approach
          // possible because of modular arithmetic
          // and cyclic nature of rot13 transform
          rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
        } else {
          rotCharArray.push(str[x].charCodeAt());
        }
      }
      str = String.fromCharCode.apply(String, rotCharArray);
      return str;
    }
    // Change the inputs below to test
    rot13("LBH QVQ VG!");
  • self

     
    function rot13(str) { // LBH QVQ VG!
      var decode = [];
      var decodeString = "";
      for (i = 0; i < str.length; i++) {
        decode.push(str.charCodeAt(i));
        if (decode[i] >= 78 && decode[i] <= 90) {
          decode[i] = decode[i] - 13;
        } else if (decode[i] >= 65 && decode[i] < 78) {
          decode[i] = decode[i] + 13;
        }
        decodeString += String.fromCharCode(decode[i]);
      }
      return decodeString;
    }
    // Change the inputs below to test
    rot13("SERR PBQR PNZC");
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    FCC
    self

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (X11; OpenBSD amd64; rv:102.0) Gecko/20100101 Firefox/102.0
Firefox 102 on OpenBSD
View result in a separate tab
Test name Executions per second
FCC 255661.5 Ops/sec
self 591354.8 Ops/sec