Skip to content
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
2 changes: 1 addition & 1 deletion benchmark/blob/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Blob } = require('buffer');

const bench = common.createBenchmark(main, {
bytes: [128, 1024, 1024 ** 2],
n: [1e6],
n: [1e3],
operation: ['text', 'arrayBuffer'],
});

Expand Down
22 changes: 22 additions & 0 deletions benchmark/blob/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');
const {
Blob,
} = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
});

let _cloneResult;

function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i)
_cloneResult = structuredClone(new Blob(['hello']));
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_cloneResult);
}
48 changes: 48 additions & 0 deletions benchmark/blob/creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const common = require('../common.js');
const {
Blob,
} = require('node:buffer');
const assert = require('assert');

const options = {
flags: ['--expose-internals'],
};

const bench = common.createBenchmark(main, {
n: [50e3],
kind: [
'Blob',
'createBlob',
],
}, options);

let _blob;
let _createBlob;

function main({ n, kind }) {
switch (kind) {
case 'Blob': {
bench.start();
for (let i = 0; i < n; ++i)
_blob = new Blob(['hello']);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_blob);
break;
} case 'createBlob': {
const { createBlob } = require('internal/blob');
const reusableBlob = new Blob(['hello']);
bench.start();
for (let i = 0; i < n; ++i)
_createBlob = createBlob(reusableBlob, reusableBlob.size);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_createBlob);
break;
} default:
throw new Error('Invalid kind');
}
}
4 changes: 2 additions & 2 deletions benchmark/blob/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const common = require('../common.js');
const { File } = require('buffer');

const bench = common.createBenchmark(main, {
bytes: [128, 1024, 1024 ** 2],
n: [1e6],
bytes: [128, 1024],
n: [1e3],
operation: ['text', 'arrayBuffer'],
});

Expand Down
22 changes: 22 additions & 0 deletions benchmark/blob/resolveObjectURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');
const { Blob, resolveObjectURL } = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
});

let _resolveObjectURL;

function main({ n }) {
const blob = new Blob(['hello']);
const id = URL.createObjectURL(blob);
bench.start();
for (let i = 0; i < n; ++i)
_resolveObjectURL = resolveObjectURL(id);
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_resolveObjectURL);
}
27 changes: 27 additions & 0 deletions benchmark/blob/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common.js');
const { Blob } = require('node:buffer');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
dataSize: [
5,
30 * 1024,
],
});

let _sliceResult;

function main({ n, dataSize }) {
const data = 'a'.repeat(dataSize);
const reusableBlob = new Blob([data]);

bench.start();
for (let i = 0; i < n; ++i)
_sliceResult = reusableBlob.slice(0, Math.floor(data.length / 2));
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_sliceResult);
}
23 changes: 17 additions & 6 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
MathMin,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
PromiseReject,
ReflectConstruct,
RegExpPrototypeExec,
Expand Down Expand Up @@ -395,13 +396,23 @@ function ClonedBlob() {
}
ClonedBlob.prototype[kDeserialize] = () => {};

function TransferrableBlob(handle, length, type = '') {
markTransferMode(this, true, false);
this[kHandle] = handle;
this[kType] = type;
this[kLength] = length;
}

ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
ObjectSetPrototypeOf(TransferrableBlob, Blob);

function createBlob(handle, length, type = '') {
return ReflectConstruct(function() {
markTransferMode(this, true, false);
this[kHandle] = handle;
this[kType] = type;
this[kLength] = length;
}, [], Blob);
const transferredBlob = new TransferrableBlob(handle, length, type);

// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
transferredBlob.constructor = Blob;

return transferredBlob;
}

ObjectDefineProperty(Blob.prototype, SymbolToStringTag, {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-blob-createobjecturl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const assert = require('assert');
const id = URL.createObjectURL(blob);
assert.strictEqual(typeof id, 'string');
const otherBlob = resolveObjectURL(id);
assert.ok(otherBlob instanceof Blob);
assert.strictEqual(otherBlob.constructor, Blob);
assert.strictEqual(otherBlob.size, 5);
assert.strictEqual(
Buffer.from(await otherBlob.arrayBuffer()).toString(),
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,10 @@ assert.throws(() => new Blob({}), {

await new Blob(chunks).arrayBuffer();
})().then(common.mustCall());

{
const blob = new Blob(['hello']);

assert.ok(blob.slice(0, 1).constructor === Blob);
assert.ok(blob.slice(0, 1) instanceof Blob);
}