Skip to content

Commit 07ed225

Browse files
committed
buffer: remove async tick and improve perf by 680%
1 parent 22c39b1 commit 07ed225

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

benchmark/blob/blob-text.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
bytes: [0, 8, 128],
6+
n: [1e6],
7+
operation: ['text', 'arrayBuffer', 'stream']
8+
});
9+
10+
function main({ n, bytes, operation }) {
11+
const buff = Buffer.allocUnsafe(bytes);
12+
const source = new Blob(buff);
13+
bench.start();
14+
for (let i = 0; i < n; i++) {
15+
switch (operation) {
16+
case 'text':
17+
source.text();
18+
break;
19+
case 'arrayBuffer':
20+
source.arrayBuffer();
21+
break;
22+
case 'stream':
23+
source.stream();
24+
break;
25+
}
26+
}
27+
bench.end(n);
28+
}

lib/internal/blob.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
PromiseResolve,
99
PromiseReject,
1010
SafePromisePrototypeFinally,
11+
PromisePrototypeThen,
1112
ReflectConstruct,
1213
RegExpPrototypeExec,
1314
RegExpPrototypeSymbolReplace,
@@ -308,13 +309,13 @@ class Blob {
308309
/**
309310
* @returns {Promise<string>}
310311
*/
311-
async text() {
312+
text() {
312313
if (!isBlob(this))
313-
throw new ERR_INVALID_THIS('Blob');
314+
return PromiseReject(new ERR_INVALID_THIS('Blob'));
314315

315316
dec ??= new TextDecoder();
316317

317-
return dec.decode(await this.arrayBuffer());
318+
return PromisePrototypeThen(this.arrayBuffer(), (value) => dec.decode(value));
318319
}
319320

320321
/**

0 commit comments

Comments
 (0)