structuredClone
On this page

structuredClone

Function

StructuredClone gives a deep copy of a struct that is no class, or of an array with a size. The result is a new object that no other name holds.

Compare examples

Each case copies an array of three numbers. The copy holds the same values, and writing to it leaves the original alone.

Example 1structuredClone(array(1., 2., 3.))
123
Components in this example
Result
[1,2,3]
Example 2structuredClone(array(0., 0.5, 1.))
00.51
Components in this example
Result
[0,0.5,1]
Example 3structuredClone(array(-1., -1., -1.))
-1-1-1
Components in this example
Result
[-1,-1,-1]

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it when a function must change an object without changing the caller's: copy first, then write to the copy.

Inputs and limits

A struct and an array are objects, so assigning one to a second name shares it; only the copy is separate. An instance of a class is not supported yet and is refused with TS8099.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

A deep copy of a value, as in JavaScript: const b = structuredClone(a) gives a struct or an array whose fields and elements are its own, so a write to b does not change a.

Syntax

declare function structuredClone<T>(value: T): T

Parameters

  • value T

Return value

T

Description

An instance of a class is not copied this way yet; build one with new.

See also

Edit this page Report a problem