<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/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));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
angular.copy() | |
JSON.parse(JSON.stringify()) |
Test name | Executions per second |
---|---|
angular.copy() | 222245.7 Ops/sec |
JSON.parse(JSON.stringify()) | 172004.7 Ops/sec |
Benchmark Overview
The provided benchmark, angular.copy (v1.2.24) vs JSON.parse(JSON.stringify())
, measures the performance difference between using the angular.copy()
function and the JSON.parse(JSON.stringify())
method for copying an array of objects in JavaScript.
What is tested on the provided JSON?
The benchmark defines two test cases:
angular.copy()
to copy the original array (origArr
) and store it in a new variable (newArr
).JSON.parse(JSON.stringify())
to copy the original array (origArr
) and store it in a new variable (newArr
).Options compared
The benchmark compares two options for copying an array of objects:
Angular Copy (angular.copy()
)
JSON Parse + Stringify (JSON.parse(JSON.stringify())
)
Other considerations
Library and Special JS Features
The benchmark uses the following library:
angular.copy()
): AngularJS is a JavaScript framework that provides various utility functions, including angular.copy()
for deep copying arrays. In this case, it's used to measure performance when working with arrays containing objects.No special JavaScript features or syntax are mentioned in the benchmark definition or execution results.
Alternatives
Other alternatives for copying arrays and objects include:
_.cloneDeep()
and _assignIn()
.Array.from()
, Object.assign()
): Provide more concise ways to create new arrays and objects.Keep in mind that the choice of alternative depends on the specific requirements and constraints of your use case.