diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2022-03-05 20:12:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-05 20:12:30 +0100 |
commit | 72d593fc5c662934ddce1ee339bfc00b68b7c4b4 (patch) | |
tree | cbd5ab84761f5a482ffd69a0660c7c36a3f46f12 /ext/web/05_base64.js | |
parent | 96dc7421ae1dccb90e0645b665f1f7df75f41fe4 (diff) |
perf(ext/web): optimize atob/btoa (#13841)
Follow up to #13839, optimizing `base64_roundtrip` ~20x (~125ms => ~6.5ms)
Diffstat (limited to 'ext/web/05_base64.js')
-rw-r--r-- | ext/web/05_base64.js | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/ext/web/05_base64.js b/ext/web/05_base64.js index 1244ecfd5..8238831f8 100644 --- a/ext/web/05_base64.js +++ b/ext/web/05_base64.js @@ -9,21 +9,10 @@ "use strict"; ((window) => { + const core = Deno.core; const webidl = window.__bootstrap.webidl; - const { - forgivingBase64Encode, - forgivingBase64Decode, - } = window.__bootstrap.infra; const { DOMException } = window.__bootstrap.domException; - const { - ArrayPrototypeMap, - StringPrototypeCharCodeAt, - ArrayPrototypeJoin, - SafeArrayIterator, - StringFromCharCode, - TypedArrayFrom, - Uint8Array, - } = window.__bootstrap.primordials; + const { TypeError } = window.__bootstrap.primordials; /** * @param {string} data @@ -36,13 +25,17 @@ prefix, context: "Argument 1", }); - - const uint8Array = forgivingBase64Decode(data); - const result = ArrayPrototypeMap( - [...new SafeArrayIterator(uint8Array)], - (byte) => StringFromCharCode(byte), - ); - return ArrayPrototypeJoin(result, ""); + try { + return core.opSync("op_base64_atob", data); + } catch (e) { + if (e instanceof TypeError) { + throw new DOMException( + "Failed to decode base64: invalid character", + "InvalidCharacterError", + ); + } + throw e; + } } /** @@ -56,20 +49,17 @@ prefix, context: "Argument 1", }); - const byteArray = ArrayPrototypeMap( - [...new SafeArrayIterator(data)], - (char) => { - const charCode = StringPrototypeCharCodeAt(char, 0); - if (charCode > 0xff) { - throw new DOMException( - "The string to be encoded contains characters outside of the Latin1 range.", - "InvalidCharacterError", - ); - } - return charCode; - }, - ); - return forgivingBase64Encode(TypedArrayFrom(Uint8Array, byteArray)); + try { + return core.opSync("op_base64_btoa", data); + } catch (e) { + if (e instanceof TypeError) { + throw new DOMException( + "The string to be encoded contains characters outside of the Latin1 range.", + "InvalidCharacterError", + ); + } + throw e; + } } window.__bootstrap.base64 = { |