Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
rot13 test
(version: 0)
Comparing performance of:
FCC vs self
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
FCC
// 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>
Run tests (2)
Previous results
Fork
Test case name
Result
FCC
self
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **rot13 test with Regular Expression and Array of ASCII character codes** ```javascript 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. 2. **rot13 test with simple loop and charCodeAt** ```javascript 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: * **Regular expression vs. simple loop**: Both methods can identify uppercase letters, but the regular expression approach is more concise and might be considered "more general" due to its modular arithmetic nature. * **Array of ASCII character codes vs. individual charCodeAt calls**: The first implementation stores the results in an array, while the second uses a single call to `charCodeAt` per iteration. **Pros and Cons** 1. **Regular expression with Array of ASCII character codes** * Pros: + More concise code + Modular arithmetic approach might be considered more efficient due to its cyclic nature * Cons: + Might be less readable for those unfamiliar with regular expressions 2. **Simple loop with charCodeAt** * Pros: + Easier to understand and maintain, especially for those without experience with regular expressions * Cons: + May be slightly slower due to the repeated `charCodeAt` calls **Library: 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: * Using a lookup table for character codes instead of modular arithmetic * Utilizing SIMD instructions (e.g., using `WebAssembly`) for faster execution on modern CPU architectures * Implementing an iterative algorithm using bitwise operations or other tricks to reduce the number of operations Keep 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.
Related benchmarks:
rot13342
rot13test
reverse
Compare prototype.reverse to for-loop
Comments
Confirm delete:
Do you really want to delete benchmark?