-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: implement structuredClone in native
Simplify the implementation by implementing it directly in C++. This improves performance and also makes structuredClone supported in custom snapshots. PR-URL: #50330 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Daeyeon Jeong <[email protected]>
- Loading branch information
1 parent
f236657
commit 5b453d4
Showing
8 changed files
with
150 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['string', 'object', 'arraybuffer'], | ||
n: [1e4], | ||
}); | ||
|
||
function main({ n, type }) { | ||
const data = []; | ||
|
||
switch (type) { | ||
case 'string': | ||
for (let i = 0; i < n; ++i) { | ||
data.push(new Date().toISOString()); | ||
} | ||
break; | ||
case 'object': | ||
for (let i = 0; i < n; ++i) { | ||
data.push({ ...process.config }); | ||
} | ||
break; | ||
case 'arraybuffer': | ||
for (let i = 0; i < n; ++i) { | ||
data.push(new ArrayBuffer(10)); | ||
} | ||
break; | ||
default: | ||
throw new Error('Unsupported payload type'); | ||
} | ||
|
||
const run = type === 'arraybuffer' ? (i) => { | ||
data[i] = structuredClone(data[i], { transfer: [ data[i] ] }); | ||
} : (i) => { | ||
data[i] = structuredClone(data[i]); | ||
}; | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; ++i) { | ||
run(i); | ||
} | ||
bench.end(n); | ||
assert.strictEqual(data.length, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
/* eslint-disable no-global-assign */ | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { | ||
structuredClone: _structuredClone, | ||
} = require('internal/structured_clone'); | ||
assert.throws(() => structuredClone(), { code: 'ERR_MISSING_ARGS' }); | ||
assert.throws(() => structuredClone(undefined, ''), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => structuredClone(undefined, 1), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => structuredClone(undefined, { transfer: 1 }), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
assert.throws(() => structuredClone(undefined, { transfer: '' }), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
|
||
const { | ||
strictEqual, | ||
throws, | ||
} = require('assert'); | ||
|
||
strictEqual(globalThis.structuredClone, _structuredClone); | ||
structuredClone = undefined; | ||
strictEqual(globalThis.structuredClone, undefined); | ||
|
||
// Restore the value for the known globals check. | ||
structuredClone = _structuredClone; | ||
|
||
throws(() => _structuredClone(), /ERR_MISSING_ARGS/); | ||
// Options can be null or undefined. | ||
assert.strictEqual(structuredClone(undefined), undefined); | ||
assert.strictEqual(structuredClone(undefined, null), undefined); | ||
// Transfer can be null or undefined. | ||
assert.strictEqual(structuredClone(undefined, { transfer: null }), undefined); | ||
assert.strictEqual(structuredClone(undefined, { }), undefined); |