Skip to content

Commit

Permalink
buffer: remove async tick and improve perf by 680%
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 11, 2022
1 parent 22c39b1 commit fe750c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
33 changes: 33 additions & 0 deletions benchmark/blob/blob-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common.js');
const { Blob } = require('buffer');

const bench = common.createBenchmark(main, {
bytes: [0, 8, 128],
n: [1e6],
operation: ['text', 'arrayBuffer', 'stream']
});

async function run(n, bytes, operation) {
const buff = Buffer.allocUnsafe(bytes);
const source = new Blob(buff);
bench.start();
for (let i = 0; i < n; i++) {
switch (operation) {
case 'text':
await source.text();
break;
case 'arrayBuffer':
await source.arrayBuffer();
break;
case 'stream':
source.stream();
break;
}
}
bench.end(n);
}

function main(conf) {
run(conf.n, conf.bytes, conf.operation).catch(console.log);
}
7 changes: 4 additions & 3 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
PromiseResolve,
PromiseReject,
SafePromisePrototypeFinally,
PromisePrototypeThen,
ReflectConstruct,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
Expand Down Expand Up @@ -308,13 +309,13 @@ class Blob {
/**
* @returns {Promise<string>}
*/
async text() {
text() {
if (!isBlob(this))
throw new ERR_INVALID_THIS('Blob');
return PromiseReject(new ERR_INVALID_THIS('Blob'));

dec ??= new TextDecoder();

return dec.decode(await this.arrayBuffer());
return PromisePrototypeThen(this.arrayBuffer(), (value) => dec.decode(value));
}

/**
Expand Down

0 comments on commit fe750c0

Please sign in to comment.