Skip to content

Commit b2761b5

Browse files
awesomeklingtrflynn89
authored andcommitted
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.
1 parent c9c98a1 commit b2761b5

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Libraries/LibJS/Runtime/Uint8Array.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,13 @@ JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayConstructorHelpers::from_base64)
115115
auto result_length = result.bytes.size();
116116

117117
// 12. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength).
118-
auto typed_array = TRY(Uint8Array::create(realm, result_length));
119-
120-
// 13. Assert: ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is the number of elements in result.[[Bytes]].
121-
VERIFY(typed_array->viewed_array_buffer()->byte_length() == result_length);
122-
123118
// 14. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
124119
// index of result.[[Bytes]].
125-
auto& array_buffer_data = typed_array->viewed_array_buffer()->buffer();
120+
auto array_buffer = ArrayBuffer::create(realm, move(result.bytes));
121+
auto typed_array = Uint8Array::create(realm, result_length, array_buffer);
126122

127-
for (size_t index = 0; index < result_length; ++index)
128-
array_buffer_data[index] = result.bytes[index];
123+
// 13. Assert: ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is the number of elements in result.[[Bytes]].
124+
VERIFY(typed_array->viewed_array_buffer()->byte_length() == result_length);
129125

130126
// 15. Return ta.
131127
return typed_array;

0 commit comments

Comments
 (0)