Skip to content

array/object comparison is not symmetric #132

Open
@davidje13

Description

@davidje13

Due to the way Arrays are identified, it is possible to construct an object which "looks like" an array, but only when passed as the second argument:

const v1 = ['a', 'b'];
const v2 = { 0: 'a', 1: 'b', 2: 'c', length: 2, constructor: Array };
equal(v1, v2); // = true
equal(v2, v1); // = false

It doesn't seem particularly problematic or risky either way, but the asymmetry of the arguments is surprising.

There is also a similar (related?) issue that any extra properties on an array are not checked:

const v1 = Object.assign(['a', 'b'], { woo: 'hi' });
const v2 = ['a', 'b'];
equal(v1, v2); // = true
equal(v2, v1); // = true

Both of these behaviours diverge from the behaviour of util.isDeepStrictEqual

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions