LibJS: Avoid redundant ByteBuffer in Uint8Array.fromBase64()

Since we already have a ByteBuffer from decoding the Base64 data, we can
pass that when creating a new ArrayBuffer.

This avoids a buffer allocation + memory clear + memory copy.
This commit is contained in:
Andreas Kling 2025-11-29 10:11:41 +01:00 committed by Tim Flynn
parent c9c98a150d
commit b2761b5640
Notes: github-actions[bot] 2025-11-29 14:41:13 +00:00

View file

@ -115,18 +115,14 @@ JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayConstructorHelpers::from_base64)
auto result_length = result.bytes.size();
// 12. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength).
auto typed_array = TRY(Uint8Array::create(realm, result_length));
// 14. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
// index of result.[[Bytes]].
auto array_buffer = ArrayBuffer::create(realm, move(result.bytes));
auto typed_array = Uint8Array::create(realm, result_length, array_buffer);
// 13. Assert: ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is the number of elements in result.[[Bytes]].
VERIFY(typed_array->viewed_array_buffer()->byte_length() == result_length);
// 14. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
// index of result.[[Bytes]].
auto& array_buffer_data = typed_array->viewed_array_buffer()->buffer();
for (size_t index = 0; index < result_length; ++index)
array_buffer_data[index] = result.bytes[index];
// 15. Return ta.
return typed_array;
}