-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
src: implement structuredClone in native #50330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 4 commits into
nodejs:main
from
joyeecheung:native-structured-clone
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b69261c
src: implement structuredClone in native
joyeecheung 571f8b9
fixup! src: implement structuredClone in native
joyeecheung f3dc8f4
fixup! fixup! src: implement structuredClone in native
joyeecheung 0a090f5
fixup! fixup! fixup! src: implement structuredClone in native
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.