Object comparison
Date tested:
4 years ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Test name
Executions per second
lodash
340578.9 Ops/sec
underscore
1012271.0 Ops/sec
JSON.stringify() JSON.st
224189.0 Ops/sec
isEqualShallow()
27907020.0 Ops/sec
Benchmark definition (click to collapse):
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script> var underscore = _.noConflict(); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.0/lodash.min.js"></script> <script> var lodash = _.noConflict(); </script> <script> Benchmark.prototype.setup = function() { var obj1 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: 5, upper: 10 }, channels: 'ZWIP' }, result: null }; var obj2 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: null, upper: null }, channels: 'ZWIP' }, result: null }; lodash = window.lodash; underscore = window.underscore window.isEqualShallow = function (a, b) { if (!a && !b) { return true; } if (!a && b || a && !b) { return false; } var numKeysA = 0, numKeysB = 0, key; for (key in b) { numKeysB++; if (/* !isPrimitive(b[key]) || */ !a.hasOwnProperty(key) || (a[key] !== b[key])) { // console.log(key, a, b); return false; } } for (key in a) { numKeysA++; } return numKeysA === numKeysB; } }; </script>
Tests:
lodash
window.lodash.isEqual(obj1, obj2);
underscore
window.underscore.isEqual(obj1, obj2);
JSON.stringify() JSON.st
JSON.stringify(obj1) == JSON.stringify(obj2);
isEqualShallow()
window.isEqualShallow(obj1, obj2);
Open this result on MeasureThat.net