Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object compare
(version: 0)
Comparing performance of:
deepEquals vs stringifyEquals vs Object.prototype.equals vs Lodash isEqual vs underscore isequal
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/underscorejs/1.5.1/underscore-min.js"></script> <script>window.underscore = _.noConflict()</script> <script src="https://cdn.jsdelivr.net/lodash/3.10.1/lodash.min.js"></script> <script>window.lodash = _.noConflict()</script>
Script Preparation code:
var base = {}; base.deepEquals = function(a, b) { if (typeof a != typeof b) { return false; } if (typeof a != "object") { return a == b; } if (a instanceof Array) { if (!(b instanceof Array)) { return false; } if (a.length != b.length) { return false; } for (var i = 0; i < a.length; i++) { if (!base.deepEquals(a[i], b[i])) { return false; } } return true; } var aKeyCount = 0; for (var key in a) { aKeyCount++; if (!(key in b)) { return false; } if (!base.deepEquals(a[key], b[key])) { return false; } } var bKeyCount = 0; for (var key in b) { if (!(key in a)) { return false; } bKeyCount++; } return aKeyCount == bKeyCount; }; Object.prototype.equals = function (x, deep) { if (deep) { if (x == this) return true; var p; for (p in this) { if (typeof (x[p]) == 'undefined') { return false; } } for (p in this) { if (this[p]) { switch (typeof (this[p])) { case 'object': if (!this[p].equals(x[p])) { return false; } break; case 'function': if (typeof (x[p]) == 'undefined' || (p != 'equals' && this[p].toString() != x[p].toString())) return false; break; default: if (this[p] != x[p]) { return false; } } } else { if (x[p]) return false; } } for (p in x) { if (typeof (this[p]) == 'undefined') { return false; } } return true; } return x == this; } base.stringifyEquals = function(a, b) { return JSON.stringify(a) === JSON.stringify(b); }; a = { tableHeight: 50, topOffsets: [1, 2, 3, 4, 5] }; b = { tableHeight: 50, topOffsets: [1, 2, 3, 4, 5] };
Tests:
deepEquals
base.deepEquals(a, b)
stringifyEquals
base.stringifyEquals(a, b)
Object.prototype.equals
a.equals(b, true)
Lodash isEqual
lodash.isEqual(a,b)
underscore isequal
underscore.isEqual(a,b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
deepEquals
stringifyEquals
Object.prototype.equals
Lodash isEqual
underscore isequal
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):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark. **Benchmark Definition** The benchmark definition consists of four test cases, each measuring the performance of a specific equality function: 1. `base.deepEquals(a, b)` 2. `base.stringifyEquals(a, b)` 3. `a.equals(b, true)` (using `Object.prototype.equals`) 4. `lodash.isEqual(a,b)` (using Lodash) 5. `underscore.isEqual(a,b)` (using Underscore) **Options Compared** Each test case compares the performance of a different equality function: * `base.deepEquals` and `lodash.isEqual` and `underscore.isEqual` compare the depth of object equality, checking if two objects have the same properties with the same values. * `base.stringifyEquals` compares the string representation of two objects using JSON.stringify. **Pros and Cons** 1. **Depth of Object Equality**: `base.deepEquals`, `lodash.isEqual`, and `underscore.isEqual` use a recursive approach to compare the depth of object equality, which can lead to slower performance for large objects. * Pros: handles nested objects correctly * Cons: can be slow for very large objects 2. **String Representation**: `base.stringifyEquals` uses JSON.stringify to compare the string representation of two objects. * Pros: fast and simple approach * Cons: may not handle certain edge cases (e.g., cyclic references) **Lodash and Underscore** Both Lodash and Underscore provide optimized equality functions that are designed to be fast and efficient. However, their implementation details may differ, affecting the performance of the benchmark. **Benchmark Results** The latest benchmark results show the number of executions per second for each test case on a Mac OS X 10.15.6 machine running Chrome 85: 1. `Object.prototype.equals`: 1116419.625 exec/sec 2. `lodash.isEqual`: 1003513.1875 exec/sec 3. `underscore.isEqual`: 621719.9375 exec/sec 4. `stringifyEquals`: 380236.53125 exec/sec 5. `deepEquals`: 448270.46875 exec/sec Overall, the results suggest that `Object.prototype.equals` is the fastest, followed by `lodash.isEqual`, and then `underscore.isEqual`. `stringifyEquals` is significantly slower due to its simple approach. Keep in mind that these results are specific to this particular benchmark and may not generalize to other scenarios or environments.
Related benchmarks:
comparing while vs for loop (v10)
Yepo_deepEqual vs. lodash.isEqual v1.1
Lodash isEqual test vs Custom Recursive Function
lodash.isEqual vs fast-deep-equal SHALLOW
Comments
Confirm delete:
Do you really want to delete benchmark?