buffer: improve btoa performance

PR-URL: https://github.com/nodejs/node/pull/52427
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Yagiz Nizipli 2024-04-11 07:41:44 -04:00 committed by GitHub
parent ee4fa77624
commit 21211a3fa9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 84 additions and 6 deletions

View file

@ -69,6 +69,7 @@ const {
kMaxLength,
kStringMaxLength,
atob: _atob,
btoa: _btoa,
} = internalBinding('buffer');
const {
constants: {
@ -1249,13 +1250,11 @@ function btoa(input) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('input');
}
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (input[n].charCodeAt(0) > 0xff)
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
const result = _btoa(`${input}`);
if (result === -1) {
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
const buf = Buffer.from(input, 'latin1');
return buf.toString('base64');
return result;
}
function atob(input) {