<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var MyObject = {
"location": {
"name": "Lobby",
"id": "70ea2d94-2d21-4d9e-ae4d-bd3b5fa76562"
},
"question": {
"text": "Which setting worked best for you today?",
"id": "0033a643-ae51-4115-b392-a3939a54a06f",
"answerOrder": [
"b564a8fd-ec39-4fdd-9531-e7875d5e43e2",
"cd8bb606-4b94-42c5-b76c-bd67df09caa6",
"0ae345a5-eb4a-4415-aab1-de3789c6700e",
"52a60c7f-f5bb-4be2-af56-59c2dd2e49d4"
]
},
"answers": [
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/cd8bb606-4b94-42c5-b76c-bd67df09caa6/31c403f4-07e7-4294-8409-3cbc5379beb3.jpg",
"ansType": 1,
"id": "cd8bb606-4b94-42c5-b76c-bd67df09caa6",
"tags": "",
"link": "",
"linkDescription": "",
"linkAction": "embed"
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/b564a8fd-ec39-4fdd-9531-e7875d5e43e2.png",
"ansType": 1,
"id": "b564a8fd-ec39-4fdd-9531-e7875d5e43e2",
"tags": ""
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/0ae345a5-eb4a-4415-aab1-de3789c6700e.png",
"ansType": 1,
"id": "0ae345a5-eb4a-4415-aab1-de3789c6700e",
"tags": ""
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/52a60c7f-f5bb-4be2-af56-59c2dd2e49d4/7737b24e-c45f-4517-a626-def4c2045ff9.jpg",
"ansType": 1,
"id": "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4",
"tags": "",
"link": "",
"linkDescription": "",
"linkAction": "embed"
}
]
};
var MyObject2 = {
"location": {
"name": "Lobby",
"id": "70ea2d94-2d21-4d9e-ae4d-bd3b5fa76562"
},
"question": {
"text": "Which setting worked best for you today?",
"id": "0033a643-ae51-4115-b392-a3939a54a06f",
"answerOrder": [
"b564a8fd-ec39-4fdd-9531-e7875d5e43e2",
"cd8bb606-4b94-42c5-b76c-bd67df09caa6",
"0ae345a5-eb4a-4415-aab1-de3789c6700e",
"52a60c7f-f5bb-4be2-af56-59c2dd2e49d4"
]
},
"answers": [
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/cd8bb606-4b94-42c5-b76c-bd67df09caa6/31c403f4-07e7-4294-8409-3cbc5379beb3.jpg",
"ansType": 1,
"id": "cd8bb606-4b94-42c5-b76c-bd67df09caa6",
"tags": "",
"link": "",
"linkDescription": "",
"linkAction": "embed"
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/b564a8fd-ec39-4fdd-9531-e7875d5e43e2.png",
"ansType": 1,
"id": "b564a8fd-ec39-4fdd-9531-e7875d5e43e2",
"tags": ""
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/0ae345a5-eb4a-4415-aab1-de3789c6700e.png",
"ansType": 1,
"id": "0ae345a5-eb4a-4415-aab1-de3789c6700e",
"tags": ""
},
{
"text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/52a60c7f-f5bb-4be2-af56-59c2dd2e49d4/7737b24e-c45f-4517-a626-def4c2045ff9.jpg",
"ansType": 1,
"id": "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4",
"tags": "",
"link": "",
"linkDescription": "",
"linkAction": "embed"
}
]
};
var myCopy = null;
_.isEqual(MyObject, MyObject2);
JSON.stringify(MyObject) === JSON.stringify(MyObject2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 263366.9 Ops/sec |
Json clone | 301980.2 Ops/sec |
In the benchmark named "Lodash isEqual vs JSON stringify1", two different approaches for comparing JavaScript objects are tested: _.isEqual
from Lodash and the native JSON.stringify
method. The aim is to evaluate the performance of these two methods in terms of execution speed.
Lodash isEqual
_.isEqual(MyObject, MyObject2);
_.isEqual
method deep compares two values to determine if they are equivalent. It will check not only the object references but also their properties and nested structures.JSON stringify
JSON.stringify(MyObject) === JSON.stringify(MyObject2);
JSON.stringify
method converts a JavaScript object into a JSON string. This string can then be compared using strict equality (===).undefined
, or circular references well; these will be ignored or throw errors.Based on the benchmark results:
isEqual
: Executions per second: 263,366.87This indicates that the JSON.stringify
method is significantly faster for the input objects being tested when compared to Lodash's isEqual
.
JSON.stringify
suffices.deep-equal
or fast-deep-equal
, which may offer similar functionality with different performance characteristics.Map
or Set
for specific cases where unique groupings or mappings of objects are needed, especially when the data sets are of simpler structures.In conclusion, this benchmark provides valuable insights into the trade-offs between accuracy and performance when comparing JavaScript objects, guiding developers in choosing the right approach suited for their specific scenarios.