GeezerVison is showing github.com — lib: reduce overhead of blob clone · nodejs/node@b6021ab
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/blob/clone.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,14 +7,17 @@ const assert = require('assert'); | 7 | 7 |
| 8 | 8 | const bench = common.createBenchmark(main, { | 9 | 9 | n: [50e3], | | 10 | + bytes: [128, 1024, 1024 ** 2], | 10 | 11 | }); | 11 | 12 |
| 12 | 13 | let _cloneResult; | 13 | 14 |
| 14 | | -function main({ n }) { | | 15 | +function main({ n, bytes }) { | | 16 | + const buff = Buffer.allocUnsafe(bytes); | | 17 | + const blob = new Blob(buff); | 15 | 18 | bench.start(); | 16 | 19 | for (let i = 0; i < n; ++i) | 17 | | - _cloneResult = structuredClone(new Blob(['hello'])); | | 20 | + _cloneResult = structuredClone(blob); | 18 | 21 | bench.end(n); | 19 | 22 |
| 20 | 23 | // Avoid V8 deadcode (elimination) | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/blob.js +5-14Lines changed: 5 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | ObjectDefineProperty, | 9 | 9 | ObjectSetPrototypeOf, | 10 | 10 | PromiseReject, | 11 | | - ReflectConstruct, | 12 | 11 | RegExpPrototypeExec, | 13 | 12 | RegExpPrototypeSymbolReplace, | 14 | 13 | StringPrototypeToLowerCase, | @@ -200,7 +199,7 @@ class Blob { | 200 | 199 | const length = this[kLength]; | 201 | 200 | return { | 202 | 201 | data: { handle, type, length }, | 203 | | - deserializeInfo: 'internal/blob:ClonedBlob', | | 202 | + deserializeInfo: 'internal/blob:Blob', | 204 | 203 | }; | 205 | 204 | } | 206 | 205 |
| @@ -397,25 +396,18 @@ class Blob { | 397 | 396 | } | 398 | 397 | } | 399 | 398 |
| 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 = '') { | 408 | 400 | markTransferMode(this, true, false); | 409 | 401 | this[kHandle] = handle; | 410 | 402 | this[kType] = type; | 411 | 403 | this[kLength] = length; | 412 | 404 | } | 413 | 405 |
| 414 | | -ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype); | 415 | | -ObjectSetPrototypeOf(TransferrableBlob, Blob); | | 406 | +ObjectSetPrototypeOf(TransferableBlob.prototype, Blob.prototype); | | 407 | +ObjectSetPrototypeOf(TransferableBlob, Blob); | 416 | 408 |
| 417 | 409 | function createBlob(handle, length, type = '') { | 418 | | - const transferredBlob = new TransferrableBlob(handle, length, type); | | 410 | + const transferredBlob = new TransferableBlob(handle, length, type); | 419 | 411 |
| 420 | 412 | // Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053 | 421 | 413 | transferredBlob.constructor = Blob; | @@ -489,7 +481,6 @@ function createBlobFromFilePath(path, options) { | 489 | 481 |
| 490 | 482 | module.exports = { | 491 | 483 | Blob, | 492 | | - ClonedBlob, | 493 | 484 | createBlob, | 494 | 485 | createBlobFromFilePath, | 495 | 486 | isBlob, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-blob.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -480,3 +480,13 @@ assert.throws(() => new Blob({}), { | 480 | 480 | assert.ok(blob.slice(0, 1).constructor === Blob); | 481 | 481 | assert.ok(blob.slice(0, 1) instanceof Blob); | 482 | 482 | } | | 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()); |
|