Skip to content

Commit b0c0746

Browse files
committed
buffer: remove async tick and improve perf by 680%
1 parent 6686d90 commit b0c0746

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

benchmark/blob/blob-text.js

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

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)