var vertices = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
]
var myCopy = null;
myCopy = JSON.parse(JSON.stringify(vertices));
myCopy = vertices.slice();
myCopy = Array.from(vertices)
myCopy = [vertices]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.stringify | |
slice | |
Array from | |
Spread |
Test name | Executions per second |
---|---|
JSON.stringify | 1442503.9 Ops/sec |
slice | 9405252.0 Ops/sec |
Array from | 5418190.5 Ops/sec |
Spread | 8670161.0 Ops/sec |
Overview
MeasureThat.net is a platform for creating and running JavaScript microbenchmarks. The provided JSON benchmark definition compares the performance of different methods for copying an array in JavaScript: JSON.stringify
, slice
, Array.from
, spread
(also known as destructuring), and structuredClone
. Let's dive into each method, their pros and cons, and other considerations.
Methods being compared
Pros and Cons of each approach:
slice
.Structured Clone
algorithm. This method is designed for cloning complex objects.slice
or spread, may not work with all data types.Libraries used
None are explicitly mentioned in the provided benchmark definition, but structuredClone
is a built-in JavaScript function introduced in ECMAScript 2022 (ES12).
Special JS features/syntax
None are explicitly mentioned in the provided benchmark definition.
Other alternatives
Benchmarking considerations
slice
or spread.Future benchmarks
In the future, MeasureThat.net could consider adding new methods to the comparison, such as:
Additionally, exploring different data structures like sets, maps, or objects of other types might provide more comprehensive results.