Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Yepo_deepEqual vs. lodash.isEqual v1.1
(version: 2)
Aim to optimize object deepEqual algorithm in JS
Comparing performance of:
lodash vs custom-deepEqual vs JSON.stringify()
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
function deepEqual(obj1, obj2){ // Initialisation let obj1Keys = Object.keys(obj1); let obj2Keys = Object.keys(obj2); // Check property number if(obj1Keys.length != obj2Keys.length) return false; // Check property key if(!obj1Keys.every(key => obj2[key])) return false; // Check property value { // Array containing both objects values for comparison let objValues = obj1Keys.map(key =>[obj1[key], obj2[key]]) // Check values type let sameType = objValues.every( value => typeof value[0] === typeof value[1]); if(!sameType) return false; // Arrays containing JS types let valueTypes = ['number', 'string', 'boolean', 'symbol', 'undefined', 'bigint']; let allTypes = [...valueTypes, 'null', 'object']; // Sort [objValues] so that value types will be processed before reference types let objValuesTypeSorted = objValues.sort((a, b) => allTypes.indexOf(typeof a[0]) - allTypes.indexOf(typeof b[0])); // NB: every() is useful here to exit the loop as fast as possible let sameValues = objValuesTypeSorted.every(values => { // Check value types if(valueTypes.includes(typeof values[0])) return values[0] === values[1]; // Check reference types else { if (values[0] === null && values[1] === null) return true; else if(values[0] === null && values[1] !== null || values[1] === null && values[0] !== null) return false; else return deepEqual(values[0], values[1]); } }) if(!sameValues) return false; } return true; } // 1 level deep window.foo1 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; window.bar1 = { a: 1, b: 3, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; // 2 levels deep window.foo2 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; window.bar2 = { a: 1, b: 2, c: { a: 1, b: 3, c: { a: 1, b: 2 } } }; // 3 levels deep window.foo3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }, d: 6 }; window.bar3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, z: 2 } } }, d: 6 }; var toto = { "data":[ { "period":"Month", "start_date":"2012-06", "end_date":"2012-07", "attributes":{ }, "measures":{ "Visits":1000000 }, "SubRows":[ { "Unknown":{ "measures":{ "Visits":1000 }, "SubRows":null }, "**":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Afghanistan":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Aland Islands":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Albania":{ "measures":{ "Visits":100 }, "SubRows":null }, } ], "year":"2002" } ] } var toto1 = { "data":[ { "period":"Month", "start_date":"2012-06", "end_date":"2012-07", "attributes":{ }, "measures":{ "Visits":1000000 }, "SubRows":[ { "Unknown":{ "measures":{ "Visits":1000 }, "SubRows":null }, "**":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Afghanistan":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Aland Islands":{ "measures":{ "Visits":1000 }, "SubRows":null }, "Albania":{ "measures":{ "Visits":100 }, "SubRows":null }, } ], "year":"2001" } ] }
Tests:
lodash
_.isEqual(toto, toto1)
custom-deepEqual
deepEqual(toto, toto1);
JSON.stringify()
JSON.stringify(toto) === JSON.stringify(toto1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
custom-deepEqual
JSON.stringify()
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to provide a clear and concise answer. The provided JSON objects `toto` and `toto1` seem to be similar, but not identical. The main difference is that `toto1` has an additional property `"data"` which contains an array of objects with more detailed information. To determine if these two objects are equal, we need to consider the following: * The structure and nesting of the properties (e.g., `"measures"`, `"SubRows"`) * The values of the properties (e.g., `1000` vs `100`) Considering these factors, it seems that `toto` and `toto1` are not exactly equal. However, to provide a more detailed analysis: * The `"data"` property in `toto1` is an array of objects with a single element, whereas `toto` does not have this property. * Within the `"data"` object in `toto1`, there is an additional property `"year"`, which is missing in `toto`. Based on these differences, it's likely that `_.isEqual(toto, toto1)` will return `false`. Similarly, `deepEqual(toto, toto1);` and `JSON.stringify(toto) === JSON.stringify(toto1)` will also return `false`.
Related benchmarks:
Deep equality
custom_deepEqual vs. lodash.isEqual
Another lodash isEqual test
Lodash vs Native JS isEqual
Comments
Confirm delete:
Do you really want to delete benchmark?