lib: add primitive fast path to structuredClone · nodejs/node@46a8a5d · GitHub
Skip to content

Commit 46a8a5d

Browse files
araujoguiaduh95
authored andcommitted
lib: add primitive fast path to structuredClone
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #64190 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 33e4ba3 commit 46a8a5d

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎benchmark/misc/structured-clone.js‎

Lines changed: 6 additions & 1 deletion

‎lib/internal/worker/js_transferable.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ function structuredClone(value, options) {
103103
throw new ERR_MISSING_ARGS('The value argument must be specified');
104104
}
105105

106+
// Fast path: primitives clone to themselves, so skip the native
107+
// serialize/deserialize round-trip when no options were provided.
108+
// Symbols are excluded. they are not cloneable and must throw below.
109+
if (options === undefined &&
110+
(value === null ||
111+
(typeof value !== 'object' &&
112+
typeof value !== 'function' &&
113+
typeof value !== 'symbol'))) {
114+
return value;
115+
}
116+
106117
const idlOptions = webidl.converters.StructuredSerializeOptions(
107118
options,
108119
{

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

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)