GeezerVison is showing github.com — lib: add primitive fast path to structuredClone · nodejs/node@fbf2ee5
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/misc/structured-clone.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,14 +4,19 @@ const common = require('../common.js'); | 4 | 4 | const assert = require('assert'); | 5 | 5 |
| 6 | 6 | const bench = common.createBenchmark(main, { | 7 | | - type: ['string', 'object', 'arraybuffer'], | | 7 | + type: ['int', 'string', 'object', 'arraybuffer'], | 8 | 8 | n: [1e4], | 9 | 9 | }); | 10 | 10 |
| 11 | 11 | function main({ n, type }) { | 12 | 12 | const data = []; | 13 | 13 |
| 14 | 14 | switch (type) { | | 15 | + case 'int': | | 16 | + for (let i = 0; i < n; ++i) { | | 17 | + data.push(i); | | 18 | + } | | 19 | + break; | 15 | 20 | case 'string': | 16 | 21 | for (let i = 0; i < n; ++i) { | 17 | 22 | data.push(new Date().toISOString()); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/worker/js_transferable.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,6 +115,17 @@ function structuredClone(value, options) { | 115 | 115 | throw new ERR_MISSING_ARGS('The value argument must be specified'); | 116 | 116 | } | 117 | 117 |
| | 118 | + // Fast path: primitives clone to themselves, so skip the native | | 119 | + // serialize/deserialize round-trip when no options were provided. | | 120 | + // Symbols are excluded. they are not cloneable and must throw below. | | 121 | + if (options === undefined && | | 122 | + (value === null || | | 123 | + (typeof value !== 'object' && | | 124 | + typeof value !== 'function' && | | 125 | + typeof value !== 'symbol'))) { | | 126 | + return value; | | 127 | + } | | 128 | + | 118 | 129 | const idlOptions = webidl.converters.StructuredSerializeOptions( | 119 | 130 | options, | 120 | 131 | { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-structuredClone-global.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,17 @@ assert.strictEqual(structuredClone(undefined, null), undefined); | 27 | 27 | // Transfer can be null or undefined. | 28 | 28 | assert.strictEqual(structuredClone(undefined, { }), undefined); | 29 | 29 |
| | 30 | +// Primitives clone to themselves. | | 31 | +assert.strictEqual(structuredClone(null), null); | | 32 | +assert.strictEqual(structuredClone(1), 1); | | 33 | +assert.strictEqual(structuredClone('x'), 'x'); | | 34 | +assert.strictEqual(structuredClone(true), true); | | 35 | +assert.strictEqual(structuredClone(10n), 10n); | | 36 | +// -0 is preserved. | | 37 | +assert.ok(Object.is(structuredClone(-0), -0)); | | 38 | +// Symbols are not cloneable and must throw. | | 39 | +assert.throws(() => structuredClone(Symbol()), { name: 'DataCloneError' }); | | 40 | + | 30 | 41 | // Transferables or its subclasses should be received with its closest transferable superclass | 31 | 42 | for (const StreamClass of [ReadableStream, WritableStream, TransformStream]) { | 32 | 43 | const original = new StreamClass(); | |
|