Bug 182634 - Web Inspector: Object.shallowEqual always fails when comparing array property values
Summary: Web Inspector: Object.shallowEqual always fails when comparing array property...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Matt Baker
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-02-08 17:54 PST by Matt Baker
Modified: 2018-02-09 12:40 PST (History)
4 users (show)

See Also:


Attachments
Patch (6.07 KB, patch)
2018-02-08 18:06 PST, Matt Baker
no flags Details | Formatted Diff | Diff
Patch (6.52 KB, patch)
2018-02-09 00:37 PST, Matt Baker
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Baker 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.
Comment 1 Radar WebKit Bug Importer 2018-02-08 18:04:18 PST
<rdar://problem/37374639>
Comment 2 Matt Baker 2018-02-08 18:06:23 PST
Created attachment 333436 [details]
Patch
Comment 3 Devin Rousso 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});
Comment 4 Matt Baker 2018-02-09 00:37:10 PST
Created attachment 333456 [details]
Patch
Comment 5 Matt Baker 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.
Comment 6 Devin Rousso 2018-02-09 12:15:42 PST
Comment on attachment 333456 [details]
Patch

r=me.  Nice fix =D
Comment 7 WebKit Commit Bot 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>
Comment 8 WebKit Commit Bot 2018-02-09 12:40:17 PST
All reviewed patches have been landed.  Closing bug.