{"ScriptPreparationCode":"var bytes = Array.from({length: 1000}, e =\u003E 0xff * Math.random() | 0);\r\nvar uint16 = new Uint16Array(bytes);\r\nvar uint8 = new Uint8Array(bytes);\r\n\r\nfunction base64ArrayBuffer(arrayBuffer) {\r\n var base64 = \u0027\u0027;\r\n var encodings = \u0027ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\u002B/\u0027;\r\n\r\n var bytes = arrayBuffer;\r\n var byteLength = bytes.byteLength;\r\n var byteRemainder = byteLength % 3;\r\n var mainLength = byteLength - byteRemainder;\r\n\r\n var a, b, c, d;\r\n var chunk;\r\n\r\n // Main loop deals with bytes in chunks of 3\r\n for (var i = 0; i \u003C mainLength; i = i \u002B 3) {\r\n // Combine the three bytes into a single integer\r\n chunk = (bytes[i] \u003C\u003C 16) | (bytes[i \u002B 1] \u003C\u003C 8) | bytes[i \u002B 2];\r\n\r\n // Use bitmasks to extract 6-bit segments from the triplet\r\n a = (chunk \u0026 16515072) \u003E\u003E 18; // 16515072 = (2^6 - 1) \u003C\u003C 18\r\n b = (chunk \u0026 258048) \u003E\u003E 12; // 258048 = (2^6 - 1) \u003C\u003C 12\r\n c = (chunk \u0026 4032) \u003E\u003E 6; // 4032 = (2^6 - 1) \u003C\u003C 6\r\n d = chunk \u0026 63; // 63 = 2^6 - 1\r\n\r\n // Convert the raw binary segments to the appropriate ASCII encoding\r\n base64 \u002B= encodings[a] \u002B encodings[b] \u002B encodings[c] \u002B encodings[d];\r\n }\r\n\r\n // Deal with the remaining bytes and padding\r\n if (byteRemainder == 1) {\r\n chunk = bytes[mainLength];\r\n\r\n a = (chunk \u0026 252) \u003E\u003E 2; // 252 = (2^6 - 1) \u003C\u003C 2\r\n\r\n // Set the 4 least significant bits to zero\r\n b = (chunk \u0026 3) \u003C\u003C 4; // 3 = 2^2 - 1\r\n\r\n base64 \u002B= encodings[a] \u002B encodings[b] \u002B \u0027==\u0027;\r\n } else if (byteRemainder == 2) {\r\n chunk = (bytes[mainLength] \u003C\u003C 8) | bytes[mainLength \u002B 1];\r\n\r\n a = (chunk \u0026 64512) \u003E\u003E 10; // 64512 = (2^6 - 1) \u003C\u003C 10\r\n b = (chunk \u0026 1008) \u003E\u003E 4; // 1008 = (2^6 - 1) \u003C\u003C 4\r\n\r\n // Set the 2 least significant bits to zero;\r\n c = (chunk \u0026 15) \u003C\u003C 2 // 15 = 2^4 - 1\r\n\r\n base64 \u002B= encodings[a] \u002B encodings[b] \u002B encodings[c] \u002B \u0027=\u0027;\r\n }\r\n \r\n return base64;\r\n}\r\n","TestCases":[{"Name":"String.fromCharCode \u0026 btoa","Code":"globalThis.btoa(String.fromCharCode.apply(null, uint16));","IsDeferred":false},{"Name":"base64ArrayBuffer function","Code":"base64ArrayBuffer(uint8);","IsDeferred":false}]}