Skip to content

Commit

Permalink
Update codecs.js (#167)
Browse files Browse the repository at this point in the history
Even faster xor functions by about 2.1x in testing, same output as before so no breaking changes but it could be even faster if it wasent forced to be the same output:

> https://gist.github.com/theogbob/89bfd228d7ec646bac14db867f33b8b2
  • Loading branch information
theogbob authored Nov 17, 2024
1 parent 5def570 commit b1e9c32
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/rewrite/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@ export const plain = {
export const xor = {
encode(str) {
if (!str) return str;
let result = "";
for (let i = 0; i < str.length; i++) {
result += i % 2 ? String.fromCharCode(str.charCodeAt(i) ^ 2) : str[i];
}
return encodeURIComponent(result);
let result = "";
let len = str.length;
for (let i = 0; i < len; i++) {
const char = str[i];
result += i % 2 ? String.fromCharCode(char.charCodeAt(0) ^ 2) : char;
}
return encodeURIComponent(result);
},
decode(str) {
if (!str) return str;
const [input, ...search] = str.split("?");
let result = "";
const decoded = decodeURIComponent(input);
for (let i = 0; i < decoded.length; i++) {
result +=
i % 2 ? String.fromCharCode(decoded.charCodeAt(i) ^ 2) : decoded[i];
}
return result + (search.length ? "?" + search.join("?") : "");
},
if (!str) return str;
str = decodeURIComponent(str);
let result = "";
let len = str.length;
for (let i = 0; i < len; i++) {
const char = str[i];
result += i % 2 ? String.fromCharCode(char.charCodeAt(0) ^ 2) : char;
}
return result;
}
};

export const base64 = {
Expand Down

0 comments on commit b1e9c32

Please sign in to comment.