function rot13(str) {
return str.split('')
.map.call(str, function(char) {
x = char.charCodeAt(0);
if (x < 65 || x > 90) {
return String.fromCharCode(x);
}
else if (x < 78) {
return String.fromCharCode(x + 13);
}
return String.fromCharCode(x - 13);
}).join('');
}
rot13("SERR PBQR PNZC");
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 | 6974.4 Ops/sec |
self | 5087.5 Ops/sec |
Let's break down the provided JSON and analyze what's being tested.
Benchmark Definition
The benchmark definition is a JavaScript function named rot13
that takes a string as input and returns the ROT13 encrypted version of the string. The encryption process involves shifting each character by 13 positions in the alphabet.
Options Compared
In this benchmark, two different implementations of the rot13
function are compared:
map()
method to apply a transformation function to each character in the string. The transformation function checks if the character's ASCII code is within certain ranges and applies the corresponding shift.Pros and Cons
map()
method's power.Library Usage
None of the provided implementations use any external libraries.
Special JS Feature/Syntax
The ROT13 encryption process uses ASCII code values to determine which characters to shift. This is a simple and widely used technique in cryptography.
Other Considerations
rot13
function, likely because it's intended for testing purposes.Alternative Approaches
Other possible approaches to implementing ROT13 encryption could include:
However, these alternatives might add unnecessary complexity and are likely not relevant to this benchmark's focus on execution speed.