Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: remove async tick and improve perf by 48% #44966

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions benchmark/blob/blob-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const common = require('../common.js');
const { Blob } = require('buffer');

const bench = common.createBenchmark(main, {
bytes: [0, 8, 128],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

128 bytes seems rather small. I guess this is mainly used for bigger values such as 128kb, 1mb and similar.

n: [1e6],
operation: ['text', 'arrayBuffer']
});

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;
}
}
bench.end(n);
anonrig marked this conversation as resolved.
Show resolved Hide resolved
}

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