// 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!");
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");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
FCC | |
self |
Test name | Executions per second |
---|---|
FCC | 255661.5 Ops/sec |
self | 591354.8 Ops/sec |
I'll break down the provided benchmark definitions and options compared, along with their pros and cons.
Benchmark Definitions
There are two individual test cases:
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;
}
This implementation uses a regular expression to identify uppercase letters and applies the Caesar cipher rotation (shifting by 13 positions) using modular arithmetic.
function rot13(str) {
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;
}
This implementation uses a simple loop to iterate through the input string, pushing each character's ASCII code into an array. It then applies the Caesar cipher rotation using conditional statements.
Options Compared
The two implementations compare the following options:
charCodeAt
per iteration.Pros and Cons
charCodeAt
callsLibrary: None
There are no libraries explicitly mentioned in the benchmark definitions.
Special JS Feature or Syntax: None
There are no special JavaScript features or syntax used beyond standard language constructs.
Other Alternatives
If you wanted to optimize this code further, some alternative approaches could be:
WebAssembly
) for faster execution on modern CPU architecturesKeep in mind that these optimizations would require more extensive knowledge of low-level programming and performance optimization techniques.
The benchmark results provided show that the two implementations have different execution rates, with the "self" (simple loop) version being faster.