<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
var obj = {
"crossKey": "EURUSD",
"dayLowPrice": 1.13045,
"dayHighPrice": 1.13535,
"previousClosePrice": 1.13215,
"priceHist1": 107190404,
"priceHist2": 250587665,
"priceHist3": 765906410,
"priceHist4": 274925633,
"sparkline": 102925375,
"spotPrice": 1.1307,
"midPrice": 1.1307,
"precision": 4,
"percentChanged": -0.128,
"threeMonthForecast": 1.08,
"sixMonthForecast": 1.04,
"tradable": true,
"deliverable": true,
"key": "EURUSD",
"displayName": "EUR USD",
"symbolId": "EURUSD",
"objectId": 1,
"assetType": "fx",
"percentChange": -0.13,
"selected": true,
"lineChart": "9991581684158672254519078266440407",
"barChart": [9, 43, 92, 47, 41, 21, 75, 32, 51, 19, 56, 97, 89, 36, 70, 2, 83, 61, 88, 47, 31, 54],
"candleChart": "3869460804391469604270058742695196",
"midPriceModel": "Price: key EURUSD, rate 1.1307, precision 4, appendFrac false, => 1.13 07 ",
"spotPriceModel": "Price: key EURUSD, rate 1.1307, precision 4, appendFrac false, => 1.13 07 ",
"crossCurrencies": ["EUR", "USD"],
"unpackedSparkLine": {
"price": 2,
"priceDirection": 1,
"fiveMinHigh": 4,
"fiveMinLow": 0,
"spike": 0,
"scale": 30
},
"priceHistory": [3, 6, 7, 6, 4, 4, 7, 14, 31, 10, 16, 17, 22, 26, 13, 19, 15, 10, 8, 6, 6, 2, 2, 1]
};
angular.copy(obj);
_.cloneDeep(obj);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Angular copy | |
Lodash Deep Clone |
Test name | Executions per second |
---|---|
Angular copy | 451504.6 Ops/sec |
Lodash Deep Clone | 181456.1 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
What is being tested?
The benchmark compares two approaches for creating a copy of an object: Angular's copy()
function (also known as angular.copy()
) and Lodash's cloneDeep()
function. The test case creates a sample object obj
with various properties, including arrays, objects, and other nested structures.
Options compared
The benchmark is testing the performance of two options:
copy()
function: This function creates a shallow copy of the original object.cloneDeep()
function: This function creates a deep copy of the original object, which means it recursively clones all nested objects and arrays.Pros and Cons of each approach
Angular's copy()
function:
Pros:
Cons:
Lodash's cloneDeep()
function:
Pros:
copy()
function for complex data structuresCons:
Library and its purpose
In this benchmark, two libraries are used:
The Angular library is used to define the copy()
function being tested, while Lodash's cloneDeep()
function is used as an alternative approach.
Special JS feature or syntax
There are no special JavaScript features or syntaxes being used in this benchmark. The code uses standard JavaScript syntax for object creation and array manipulation.
Other alternatives
If you want to test other approaches for creating a copy of an object, some alternatives could be:
Object.assign()
method: This method creates a shallow copy of an object by copying its enumerable own properties.JSON.parse(JSON.stringify(obj))
trick: This trick creates a deep copy of an object by serializing it to a JSON string and then parsing it back into an object.Keep in mind that these alternatives may have different performance characteristics or requirements compared to Angular's copy()
function and Lodash's cloneDeep()
function.