Skip to content

Commit b6021ab

Browse files
H4adtargos
authored andcommitted
lib: reduce overhead of blob clone
PR-URL: #50110 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent bac872c commit b6021ab

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

benchmark/blob/clone.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const assert = require('assert');
77

88
const bench = common.createBenchmark(main, {
99
n: [50e3],
10+
bytes: [128, 1024, 1024 ** 2],
1011
});
1112

1213
let _cloneResult;
1314

14-
function main({ n }) {
15+
function main({ n, bytes }) {
16+
const buff = Buffer.allocUnsafe(bytes);
17+
const blob = new Blob(buff);
1518
bench.start();
1619
for (let i = 0; i < n; ++i)
17-
_cloneResult = structuredClone(new Blob(['hello']));
20+
_cloneResult = structuredClone(blob);
1821
bench.end(n);
1922

2023
// Avoid V8 deadcode (elimination)

lib/internal/blob.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
ObjectDefineProperty,
99
ObjectSetPrototypeOf,
1010
PromiseReject,
11-
ReflectConstruct,
1211
RegExpPrototypeExec,
1312
RegExpPrototypeSymbolReplace,
1413
StringPrototypeToLowerCase,
@@ -200,7 +199,7 @@ class Blob {
200199
const length = this[kLength];
201200
return {
202201
data: { handle, type, length },
203-
deserializeInfo: 'internal/blob:ClonedBlob',
202+
deserializeInfo: 'internal/blob:Blob',
204203
};
205204
}
206205

@@ -397,25 +396,18 @@ class Blob {
397396
}
398397
}
399398

400-
function ClonedBlob() {
401-
return ReflectConstruct(function() {
402-
markTransferMode(this, true, false);
403-
}, [], Blob);
404-
}
405-
ClonedBlob.prototype[kDeserialize] = () => {};
406-
407-
function TransferrableBlob(handle, length, type = '') {
399+
function TransferableBlob(handle, length, type = '') {
408400
markTransferMode(this, true, false);
409401
this[kHandle] = handle;
410402
this[kType] = type;
411403
this[kLength] = length;
412404
}
413405

414-
ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
415-
ObjectSetPrototypeOf(TransferrableBlob, Blob);
406+
ObjectSetPrototypeOf(TransferableBlob.prototype, Blob.prototype);
407+
ObjectSetPrototypeOf(TransferableBlob, Blob);
416408

417409
function createBlob(handle, length, type = '') {
418-
const transferredBlob = new TransferrableBlob(handle, length, type);
410+
const transferredBlob = new TransferableBlob(handle, length, type);
419411

420412
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
421413
transferredBlob.constructor = Blob;
@@ -489,7 +481,6 @@ function createBlobFromFilePath(path, options) {
489481

490482
module.exports = {
491483
Blob,
492-
ClonedBlob,
493484
createBlob,
494485
createBlobFromFilePath,
495486
isBlob,

test/parallel/test-blob.js

+10
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,13 @@ assert.throws(() => new Blob({}), {
480480
assert.ok(blob.slice(0, 1).constructor === Blob);
481481
assert.ok(blob.slice(0, 1) instanceof Blob);
482482
}
483+
484+
(async () => {
485+
const blob = new Blob(['hello']);
486+
487+
assert.ok(structuredClone(blob).constructor === Blob);
488+
assert.ok(structuredClone(blob) instanceof Blob);
489+
assert.ok(structuredClone(blob).size === blob.size);
490+
assert.ok(structuredClone(blob).size === blob.size);
491+
assert.ok((await structuredClone(blob).text()) === (await blob.text()));
492+
})().then(common.mustCall());

0 commit comments

Comments
 (0)