Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings.(abcde)
(version: 1)
Test on isEqual performance
Comparing performance of:
_.isEqual vs JSON.stringify vs areValuesEqual
Created:
one year ago
by:
Guest
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:
window.foo = ['cat', 'dog', 'bird']; window.bar = ['cat', 'dog', 'bird']; // Simple circular detection, shouldn't be a problem in react-hook-form values. // Hopefull 1_000 is deeper than we'll ever actually go... const DEPTH_LIMIT = 1_000; const isPlainObject = (thing) => Boolean( thing && typeof thing === 'object' && ( thing.constructor === Object || thing.constructor === null ), ); const areValuesEqualInternal = (referenceValue, checkValue, depth) => { if (referenceValue === checkValue) { return true; } if ( depth === DEPTH_LIMIT || referenceValue === null || checkValue === null || referenceValue === undefined || checkValue === undefined ) { return false; } const referenceIsArray = Array.isArray(referenceValue); const checkIsArray = Array.isArray(checkValue); if (referenceIsArray && checkIsArray) { if (referenceValue.length !== checkValue.length) { return false; } for (let i = 0; i < referenceValue.length; i += 1) { if (!areValuesEqualInternal(referenceValue[i], checkValue[i], depth + 1)) { return false; } } return true; } // if either value is an array, but the other is not, they are not equal if (referenceIsArray || checkIsArray) { return false; } if (isPlainObject(referenceValue) && isPlainObject(checkValue)) { // check all keys in the reference value are in the check // missing keys in the check are ok if the reference value is undefined const referenceKeys = Object.keys(referenceValue); for (let i = 0; i < referenceKeys.length; i += 1) { const key = referenceKeys[i]; if (!areValuesEqualInternal(referenceValue[key], checkValue[key], depth + 1)) { return false; } } // check if there are any keys in checkValue (not undefined value), but not in referenceValue const checkKeys = Object.keys(checkValue); for (let i = 0; i < checkKeys.length; i += 1) { const key = checkKeys[i]; if (checkValue[key] !== undefined && !(key in referenceValue)) { return false; } } return true; } return false; }; const areValuesEqual = (referenceValue, checkValue) => { if (referenceValue === checkValue) { return true; } return areValuesEqualInternal(referenceValue, checkValue, 1); }; window.areValuesEqual = areValuesEqual
Tests:
_.isEqual
_.isEqual(window.foo, window.bar)
JSON.stringify
JSON.stringify(window.foo) === JSON.stringify(window.bar);
areValuesEqual
areValuesEqual(window.foo, window.bar)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
areValuesEqual
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.isEqual
6027677.0 Ops/sec
JSON.stringify
3493685.5 Ops/sec
areValuesEqual
31942802.0 Ops/sec
Related benchmarks:
Deep equality
JSON.srtingify vs react-fast-compare vs _.isEqual
JSON.stringify vs react-fast-compare vs _.isEqual
react-fast-compare vs JSON.stringify Equality Comparison for Shallow Array of Strings.
react shallowEqual vs ramda
react shallowEqual vs Ramda equals
(JSON.stringify vs lodash vs fast-deep-equal vs fast-equals) with React and Promises
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Object of Strings.
Lodash.isEqual vs JSON.stringify Equality Deep Comparison for Shallow Array of Strings but comparing to a hardcoded string
Comments
Confirm delete:
Do you really want to delete benchmark?