From bb5b58614cfb811f7201cd8f16a6470d1d1c9e54 Mon Sep 17 00:00:00 2001 From: Zwyx <29386932+Zwyx@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:57:58 +0800 Subject: [PATCH 1/4] Fix error in `bytesToBase64` code example Using `fromCodePoint(...bytes)` leads to the error `RangeError: Maximum call stack size exceeded` when `bytes` is long (it happens with 150k bytes for me with Chrome 122 on Linux). Using `Array.from` fixes this issue. --- files/en-us/glossary/base64/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/glossary/base64/index.md b/files/en-us/glossary/base64/index.md index f421e4436c228d1..f398da1aab0e1ed 100644 --- a/files/en-us/glossary/base64/index.md +++ b/files/en-us/glossary/base64/index.md @@ -52,7 +52,7 @@ function base64ToBytes(base64) { } function bytesToBase64(bytes) { - const binString = String.fromCodePoint(...bytes); + const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join(""); return btoa(binString); } From 07287b88f564e782e90173b4a99c450dc1428791 Mon Sep 17 00:00:00 2001 From: Zwyx <29386932+Zwyx@users.noreply.github.com> Date: Sat, 2 Mar 2024 11:03:43 +0800 Subject: [PATCH 2/4] Mention that `FileReader` and `fetch` offer better perfomances --- files/en-us/glossary/base64/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/glossary/base64/index.md b/files/en-us/glossary/base64/index.md index f398da1aab0e1ed..a4b0c0569742e00 100644 --- a/files/en-us/glossary/base64/index.md +++ b/files/en-us/glossary/base64/index.md @@ -65,7 +65,7 @@ new TextDecoder().decode(base64ToBytes("YSDEgCDwkICAIOaWhyDwn6aE")); // "a Ā The `bytesToBase64` and `base64ToBytes` functions in the previous section can be used directly to convert between Base64 strings and [`Uint8Array`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)s. -Alternatively, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs: +For better performances, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs: ```js async function bytesToBase64DataUrl(bytes, type = "application/octet-stream") { From 7cf94f6a3c75d63d3c4416ffa0b5aeb766b0248f Mon Sep 17 00:00:00 2001 From: Zwyx <29386932+Zwyx@users.noreply.github.com> Date: Sat, 2 Mar 2024 12:20:09 +0800 Subject: [PATCH 3/4] Fix formatting Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- files/en-us/glossary/base64/index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/files/en-us/glossary/base64/index.md b/files/en-us/glossary/base64/index.md index a4b0c0569742e00..6d95e1cee395e72 100644 --- a/files/en-us/glossary/base64/index.md +++ b/files/en-us/glossary/base64/index.md @@ -52,7 +52,9 @@ function base64ToBytes(base64) { } function bytesToBase64(bytes) { - const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join(""); + const binString = Array.from(bytes, (byte) => + String.fromCodePoint(byte), + ).join(""); return btoa(binString); } From aa68dda3c4eb5ba59b501dd11b8d8fcf251f77f3 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 3 Mar 2024 00:35:15 -0500 Subject: [PATCH 4/4] Update index.md --- files/en-us/glossary/base64/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/glossary/base64/index.md b/files/en-us/glossary/base64/index.md index 6d95e1cee395e72..7912c4d13afbd3f 100644 --- a/files/en-us/glossary/base64/index.md +++ b/files/en-us/glossary/base64/index.md @@ -67,7 +67,7 @@ new TextDecoder().decode(base64ToBytes("YSDEgCDwkICAIOaWhyDwn6aE")); // "a Ā The `bytesToBase64` and `base64ToBytes` functions in the previous section can be used directly to convert between Base64 strings and [`Uint8Array`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)s. -For better performances, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs: +For better performance, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs: ```js async function bytesToBase64DataUrl(bytes, type = "application/octet-stream") {