<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body>
</body>
</html>
var origArr = [
{
name : "obj1",
val : 1,
color : "blue"
},
{
name : "obj2",
val : 2,
color : "red"
},
{
name : "obj3",
val : 3,
color : "green"
},
{
name : "obj4",
val : 4,
color : "orange"
}
];
var newArr = [];
newArr = angular.copy(origArr);
newArr = JSON.parse(JSON.stringify(origArr));
newArr = structuredClone(origArr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
angular.copy() | |
JSON.parse(JSON.stringify()) | |
structuredClone(origArr) |
Test name | Executions per second |
---|---|
angular.copy() | 772346.3 Ops/sec |
JSON.parse(JSON.stringify()) | 265098.7 Ops/sec |
structuredClone(origArr) | 141748.2 Ops/sec |
Overview
The provided JSON represents a benchmark test on MeasureThat.net, which compares the performance of three different methods for copying an array of objects: angular.copy()
, JSON.parse(JSON.stringify())
, and structuredClone()
. The goal is to determine which method provides the most efficient way to create a deep copy of the array.
Test Cases
There are three test cases:
angular.copy()
: This method uses Angular's copy
function to create a new array with a deep copy of each object in the original array.JSON.parse(JSON.stringify())
: This method uses the JSON.stringify()
function to serialize the original array, and then parses the resulting string back into an object using JSON.parse()
. The JSON.stringify()
function creates a deep copy of each object in the original array, but this method is more computationally expensive due to the additional serialization step.structuredClone()
: This method uses the structuredClone()
function, which was introduced in ECMAScript 2022, to create a deep copy of the original array.Options Compared
The three methods are compared based on their performance, with the goal of determining which one is the fastest.
Pros and Cons of Each Method
angular.copy()
:JSON.parse(JSON.stringify())
:structuredClone()
:Library Used
The angular.copy()
method uses AngularJS's copy
function, which is a utility function for creating deep copies of objects. The JSON.parse(JSON.stringify())
method relies on the JSON.stringify()
function to serialize the original array, and then parses the resulting string back into an object using JSON.parse()
. The structuredClone()
method uses the structuredClone()
function, which is a new function introduced in ECMAScript 2022 for creating deep copies of objects.
Special JavaScript Feature/Syntax
The structuredClone()
method uses a special JavaScript feature known as "structured clone", which was introduced in ECMAScript 2022. This feature allows for more efficient and secure cloning of complex data structures, such as arrays and objects.
Other Alternatives
If the angular.copy()
method is not available or is not preferred, other alternatives can be used to create a deep copy of an array, such as:
slice()
method with a shallow copy: newArr = origArr.slice();
concat()
method with a shallow copy: newArr = origArr.concat();
cloneDeep
functionHowever, these alternatives may not be as efficient or reliable as the methods tested on MeasureThat.net.