RESOLVED FIXED 182634
Web Inspector: Object.shallowEqual always fails when comparing array property values
https://bugs.webkit.org/show_bug.cgi?id=182634
Summary Web Inspector: Object.shallowEqual always fails when comparing array property...
Matt Baker
Reported 2018-02-08 17:54:44 PST
Summary: Object.shallowEqual always fails when comparing array property values. Steps to Reproduce: 1. Inspect the Inspector 2. In the console, evaluate: > Object.shallowEqual({a: []}, {a: []}); > Object.shallowEqual({a: new Array}, {a: new Array}); > Object.shallowEqual({a: [1]}, {a: [1]}); Expected: All true Actual: All false Notes: Object.shallowEqual uses Array.shallowEqual when both objects are arrays, but always uses strict equality when comparing property values.
Attachments
Patch (6.07 KB, patch)
2018-02-08 18:06 PST, Matt Baker
no flags
Patch (6.52 KB, patch)
2018-02-09 00:37 PST, Matt Baker
no flags
Radar WebKit Bug Importer
Comment 1 2018-02-08 18:04:18 PST
Matt Baker
Comment 2 2018-02-08 18:06:23 PST
Devin Rousso
Comment 3 2018-02-08 22:27:44 PST
Comment on attachment 333436 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=333436&action=review > Source/WebInspectorUI/UserInterface/Base/Utilities.js:92 > + let aValue = a[aKey]; > + let bValue = b[aKey]; > + if (Array.isArray(aValue) && Array.isArray(bValue)) { > + // Use Array.shallowEqual, since struct equality fails for similar arrays. > + if (!Array.shallowEqual(aValue, bValue)) > + return false; > + } else { > + // Check that the values are strict equal since this is only > + // a shallow check, not a recursive one. > + if (aValue !== bValue) > + return false; > + } I think you could simplify this to always call `Array.shallowEqual` regardless of whether `aValue` or `bValue` is an array, since it already early returns false if either is not an array. (along these lines, we could also rework the logic earlier in `Object.shallowEqual` that checks if the values are arrays to simply just call `Array.shallowEqual` and return true if it does) if (aValue !== bValue && !Array.shallowEqual(aValue, bValue) return false; I think we still want to do a strict equality comparison even for arrays, as that might help us avoid unnecessary work if it is the case that two different objects have a reference to the same array. let a = []; for (let i = 0; i < 10000; ++i) a.push(i); Object.shallowEquals({a}, {a});
Matt Baker
Comment 4 2018-02-09 00:37:10 PST
Matt Baker
Comment 5 2018-02-09 00:44:27 PST
Comment on attachment 333436 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=333436&action=review >> Source/WebInspectorUI/UserInterface/Base/Utilities.js:92 >> + } > > I think you could simplify this to always call `Array.shallowEqual` regardless of whether `aValue` or `bValue` is an array, since it already early returns false if either is not an array. (along these lines, we could also rework the logic earlier in `Object.shallowEqual` that checks if the values are arrays to simply just call `Array.shallowEqual` and return true if it does) > > if (aValue !== bValue && !Array.shallowEqual(aValue, bValue) > return false; > > I think we still want to do a strict equality comparison even for arrays, as that might help us avoid unnecessary work if it is the case that two different objects have a reference to the same array. > > let a = []; > for (let i = 0; i < 10000; ++i) > a.push(i); > > Object.shallowEquals({a}, {a}); Nice suggestions, I simplified the patch a bit, and removed redundant comments. We shouldn't need to do an additional check for two property values that refer to the same array. Array.shallowEqual performs this check early on.
Devin Rousso
Comment 6 2018-02-09 12:15:42 PST
Comment on attachment 333456 [details] Patch r=me. Nice fix =D
WebKit Commit Bot
Comment 7 2018-02-09 12:40:15 PST
Comment on attachment 333456 [details] Patch Clearing flags on attachment: 333456 Committed r228336: <https://trac.webkit.org/changeset/228336>
WebKit Commit Bot
Comment 8 2018-02-09 12:40:17 PST
All reviewed patches have been landed. Closing bug.
Note You need to log in before you can comment on or make changes to this bug.