summaryrefslogtreecommitdiff
path: root/ext/web/09_file.js
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-03-16 19:16:52 +0530
committerGitHub <noreply@github.com>2022-03-16 19:16:52 +0530
commit7044bf523bab3a6da47150a05823dd6571abfd49 (patch)
tree48967a64ec68b805b91ec14dcdbe954b10563f7c /ext/web/09_file.js
parentbfa4ed92bcd119fa5c8fdb6b444b83a092dccca9 (diff)
perf(web): optimize Blob.text and Blob.arrayBuffer (#13981)
Diffstat (limited to 'ext/web/09_file.js')
-rw-r--r--ext/web/09_file.js28
1 files changed, 18 insertions, 10 deletions
diff --git a/ext/web/09_file.js b/ext/web/09_file.js
index 2117a0835..50cce9004 100644
--- a/ext/web/09_file.js
+++ b/ext/web/09_file.js
@@ -340,8 +340,22 @@
*/
async text() {
webidl.assertBranded(this, BlobPrototype);
- const buffer = await this.arrayBuffer();
- return core.decode(new Uint8Array(buffer));
+ const buffer = await this.#u8Array(this.size);
+ return core.decode(buffer);
+ }
+
+ async #u8Array(size) {
+ const bytes = new Uint8Array(size);
+ const partIterator = toIterator(this[_parts]);
+ let offset = 0;
+ for await (const chunk of partIterator) {
+ const byteLength = chunk.byteLength;
+ if (byteLength > 0) {
+ TypedArrayPrototypeSet(bytes, chunk, offset);
+ offset += byteLength;
+ }
+ }
+ return bytes;
}
/**
@@ -349,14 +363,8 @@
*/
async arrayBuffer() {
webidl.assertBranded(this, BlobPrototype);
- const stream = this.stream();
- const bytes = new Uint8Array(this.size);
- let offset = 0;
- for await (const chunk of stream) {
- TypedArrayPrototypeSet(bytes, chunk, offset);
- offset += chunk.byteLength;
- }
- return bytes.buffer;
+ const buf = await this.#u8Array(this.size);
+ return buf.buffer;
}
[SymbolFor("Deno.customInspect")](inspect) {