GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lib: add primitive fast path to structuredClone · nodejs/node@fbf2ee5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/misc
    • structured-clone.js
  • lib/internal/worker
    • js_transferable.js
  • test/parallel
    • test-structuredClone-global.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/misc/structured-clone.js‎

Copy file name to clipboardExpand all lines: benchmark/misc/structured-clone.js
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ const common = require('../common.js');
44
const assert = require('assert');
55

66
const bench = common.createBenchmark(main, {
7-
type: ['string', 'object', 'arraybuffer'],
7+
type: ['int', 'string', 'object', 'arraybuffer'],
88
n: [1e4],
99
});
1010

1111
function main({ n, type }) {
1212
const data = [];
1313

1414
switch (type) {
15+
case 'int':
16+
for (let i = 0; i < n; ++i) {
17+
data.push(i);
18+
}
19+
break;
1520
case 'string':
1621
for (let i = 0; i < n; ++i) {
1722
data.push(new Date().toISOString());
Collapse file

‎lib/internal/worker/js_transferable.js‎

Copy file name to clipboardExpand all lines: lib/internal/worker/js_transferable.js
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ function structuredClone(value, options) {
115115
throw new ERR_MISSING_ARGS('The value argument must be specified');
116116
}
117117

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+
118129
const idlOptions = webidl.converters.StructuredSerializeOptions(
119130
options,
120131
{
Collapse file

‎test/parallel/test-structuredClone-global.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-structuredClone-global.js
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ assert.strictEqual(structuredClone(undefined, null), undefined);
2727
// Transfer can be null or undefined.
2828
assert.strictEqual(structuredClone(undefined, { }), undefined);
2929

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+
3041
// Transferables or its subclasses should be received with its closest transferable superclass
3142
for (const StreamClass of [ReadableStream, WritableStream, TransformStream]) {
3243
const original = new StreamClass();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.