Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deep comparsions
(version: 0)
Comparing performance of:
deepequal vs isEqual
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
// Unsorted Array number var object1 = { a: 1, b: { c: 2, d: [3, 4], }, }; var object2 = { a: 1, b: { c: 2, d: [4, 3], }, }; // Unsorted Array of objects var object3 = { a: 1, b: { c: 2, d: [{ w: 123 }, { x: 444 }], }, }; var object4 = { a: 1, b: { c: 2, d: [{ x: 444 }, { w: 123 }], }, }; // unsorted Array of Arrays of numbers var object5 = { a: 1, b: { c: 2, d: [ [1, 2, 3], [4, 5, 6], ], }, }; var object6 = { a: 1, b: { c: 2, d: [ [4, 5, 6], [1, 2, 3], ], }, };
Tests:
deepequal
const deepEqual = (obj1, obj2) => { if (obj1 === obj2) return true; if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) { return false; } if (Array.isArray(obj1) && Array.isArray(obj2)) { if (obj1.length !== obj2.length) return false; obj1 = obj1.slice().sort(); obj2 = obj2.slice().sort(); return obj1.every((value, index) => deepEqual(value, obj2[index])); } if (Array.isArray(obj1) !== Array.isArray(obj2)) { return false; } const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) return false; keys1.sort(); keys2.sort(); return keys1.every((key, index) => { if (key !== keys2[index]) return false; return deepEqual(obj1[key], obj2[key]); }); } console.log(deepEqual(object1, object2)); // true console.log(deepEqual(object3, object4)); // false console.log(deepEqual(object5, object6)); // true
isEqual
const customizer = (objValue, othValue) => { if (_.isArray(objValue) && _.isArray(othValue)) { return _.isEqual(deepSort(objValue), deepSort(othValue)); } }; const deepSort = (obj) => { if (_.isArray(obj)) { return _.sortBy(obj.map(deepSort), (item) => JSON.stringify(item)); } else if (_.isObject(obj)) { return _.keys(obj) .sort() .reduce((result, key) => { result[key] = deepSort(obj[key]); return result; }, {}); } return obj; }; console.log(_.isEqualWith(object1, object2, customizer)); // true console.log(_.isEqualWith(object3, object4, customizer)); // true console.log(_.isEqualWith(object5, object6, customizer)); // true
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
deepequal
isEqual
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
deepequal
71374.2 Ops/sec
isEqual
34942.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared, and discussed. **What is being tested?** MeasureThat.net is testing two JavaScript functions: `deepEqual` and `isEqualWith`. These functions are used to compare objects for equality. **DeepEqual function:** The `deepEqual` function checks if two objects (`obj1` and `obj2`) are deeply equal. It means that: * If the objects are not of the same type (e.g., one is an array, and the other is an object), they are considered unequal. * If both objects are arrays or objects, but their lengths or keys are different, they are considered unequal. * If both objects are arrays or objects with the same length and keys, but the values at each key are not equal, the function returns `false`. The test case uses a simple object comparison: ```javascript const deepEqual = (obj1, obj2) => { // ... } console.log(deepEqual(object1, object2)); // true ``` **isEqualWith customizer:** The `isEqualWith` function is similar to `deepEqual`, but it uses a customizer function (`customizer`) that can modify the objects being compared. This allows for more flexibility in comparing complex data structures. In this test case, the customizer simply sorts and deep-merges the objects: ```javascript const customizer = (objValue, othValue) => { if (_.isArray(objValue) && _.isArray(othValue)) { return _.isEqual(deepSort(objValue), deepSort(othValue)); } } console.log(_.isEqualWith(object1, object2, customizer)); // true ``` **Comparison and pros/cons:** The `deepEqual` function is a simple and straightforward way to compare objects for equality. However, it can be slow for large objects due to the recursive comparison of every property. On the other hand, `isEqualWith` with a customizer provides more flexibility but also introduces additional overhead from the customizer function. **Pros/cons:** * `deepEqual`: + Pros: Simple and fast. + Cons: May not work correctly for large objects or complex data structures. * `isEqualWith` with customizer: + Pros: More flexible and can handle complex data structures, but may be slower due to the customizer function. + Cons: Additional overhead from the customizer function. **Other considerations:** The test cases use Lodash (`_`) for utility functions like `_isArray`, `_isEqual`, and `_sortBy`. This suggests that the tests are using a specific library or framework that provides these utilities. Additionally, the benchmark results show different execution rates for `deepequal` and `isEqualWith`, indicating that the customizer function in `isEqualWith` may be slower than the simple comparison in `deepEqual`. I hope this explanation helps!
Related benchmarks:
PlainJS vs Lodash
off arrays function
Object.values Array.prototype.map vs Lodash.map
Deep merge lodash vs ramda vs deepmerge vs Immutable
Comments
Confirm delete:
Do you really want to delete benchmark?