GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util.isDeepStrictEqual would be better to return false to compare WeakMap/WeakSet · Issue #18227 · nodejs/node

util.isDeepStrictEqual would be better to return false to compare WeakMap/WeakSet #18227

New issue
Copy link
New issue
Copy link
Closed
Closed
util.isDeepStrictEqual would be better to return false to compare WeakMap/WeakSet#18227
Copy link
Labels
help wantedIssues that need assistance from volunteers or PRs that need help to proceed.utilIssues and PRs related to the built-in util module.

Description

@yosuke-furukawa
yosuke-furukawa
opened on Jan 18, 2018
Issue body actions
  • Version:
    v9.4.0

  • Platform:
    OSX, Linux, Windows

  • Subsystem:
    N/A

util.isDeepStrictEqual always returns true when WeakMap/WeakSet comparison.

const util = require('util');
const wm1 = new WeakMap();
const wm2 = new WeakMap();
const key = {};
wm1.set(key, 1);
wm2.set(key, 2);
console.log(util.isDeepStrictEqual(wm1, wm2)); // true

this is because WeakMap|WeakSet.prototype.valueOf always returns empty object.
I know WeakMap / WeakSet do not have an API to show the full content, and they depends on GC so those collection comparison is very difficult.

However, this behavior (always return true) seems to be curious for me. I think it would be better to always return false.

Reacted by Matteo Collina and Okiki Ojo

Activity

  1. BridgeAR commented on Jan 18, 2018

    @BridgeAR
    BridgeAR
    on Jan 18, 2018
    Member
    More actions

    I thought about this before and I feel like the current way is the more correct one.

    The comparison always relies on what is possible to detect. Since Weap(Map|Set) entries do not provide a way to compare them, they should just be ignored.

    The suggested change will also make it impossible to distinguish differences like these:

    const a = new WeakMap();
    const b = new WeakMap();
    const c = new WeakMap();
    c.isClearlyDifferent = true;
    
    util.isDeepStrictEqual(a, b); // => false even though they are indeed equal
    util.isDeepStrictEqual(a, c); // => false without distinguishing this from the former check
  2. yosuke-furukawa commented on Jan 18, 2018

    @yosuke-furukawa
    yosuke-furukawa
    on Jan 18, 2018
    MemberAuthor
    More actions

    hm, but I think WeakMap / WeakSet are not suitable to compare the equivalence, so we would be better to return false than true.

    Any WeakMap and WeakSet comparison always return true is very confusing. false is also confusing but it has the reason (WeakMap and WeakSet is not suitable to compare).

    Anyway, we would be better to add a note on documentation.

    Reacted by Benjamin Gruenbaum and Anna Henningsen
  3. BridgeAR commented on Jan 18, 2018

    @BridgeAR
    BridgeAR
    on Jan 18, 2018
    Member
    More actions

    @yosuke-furukawa I am definitely +1 for updating the docs! That should also apply to assert.deepStrictEqual.

    About being right or wrong: I think there is no definite right approach. Only one way we want to decide on. So far it was the one in place right now (maybe by chance) and I personally feel it is the better one.

    I feel returning true is not so confusing anymore after checking the documentation how weak entries are saved and after thinking about how it is possible to compare things in JS.

  4. yosuke-furukawa commented on Jan 18, 2018

    @yosuke-furukawa
    yosuke-furukawa
    on Jan 18, 2018
    MemberAuthor
    More actions

    +1 for updating the docs

    Thank you, I will send another PR.

    @nodejs/collaborators
    I would like to hear other members' opinion.
    My opinion is "WeakMap/Set are not comparable, so util.isDeepStrictEqual would be better to return false than return true".

  5. benjamingr commented on Jan 21, 2018

    @benjamingr
    benjamingr
    on Jan 21, 2018
    Member
    More actions

    Without reading the discussion (so I stay with intuition) - I'd expect deepEqual to return true if and only if the two WeakMap or WeakSets are to the same reference.

    Reacted by Ron Korving, Anna Henningsen, Yosuke Furukawa and Nikita Skovoroda
  6. BridgeAR commented on Jan 22, 2018

    @BridgeAR
    BridgeAR
    on Jan 22, 2018
    Member
    More actions

    I ask everyone to continue discussing this in #18248. Thanks :-)

    @benjamingr that would be a special handling of those and not be aligned with any other object.

    Reacted by Anna Henningsen
  7. addaleax
    added
    utilIssues and PRs related to the built-in util module.
    tsc-review
    on Feb 7, 2018
  8. addaleax commented on Feb 7, 2018

    @addaleax
    addaleax
    on Feb 7, 2018
    Member
    More actions

    /cc @nodejs/tsc

  9. ofrobots commented on Feb 7, 2018

    @ofrobots
    ofrobots
    on Feb 7, 2018
    Contributor
    More actions

    I'm +1 to what @benjamingr proposed above.

  10. mcollina commented on Feb 8, 2018

    @mcollina
    mcollina
    on Feb 8, 2018
    SponsorMember
    More actions

    I am 👍 to:

    1. return true if the two objects are the same instance and return false otherwise
    2. throw consistently because two Weak* are not really comparable

    We should also document the current behvior in our current/lts release lines.

    Reacted by Ron Korving and Jeremiah Senkpiel
  11. cjihrig commented on Feb 8, 2018

    @cjihrig
    cjihrig
    on Feb 8, 2018
    Contributor
    More actions

    I agree with @mcollina.

  12. Trott
    removed
    tsc-review
    on Mar 10, 2019
  13. wza62692371 commented on Mar 28, 2020

    @wza62692371
    wza62692371
    on Mar 28, 2020
    More actions

    @https://github.com/Trott

  14. jasnell
    added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    on Jun 26, 2020
  15. BridgeAR commented on Jun 18, 2024

    @BridgeAR
    BridgeAR
    on Jun 18, 2024
    Member
    More actions

    After looking into this again, I changed my mind / I agree with the suggested behavior change.

  16. yosuke-furukawa commented on Jun 18, 2024

    @yosuke-furukawa
    yosuke-furukawa
    on Jun 18, 2024
    MemberAuthor
    More actions

    wow. I am surprised this issue is still open.
    #53495 this PR pushed this discussion. I don't change my mind. if the PR is merged, this issue will be closed.

  17. benjamingr commented on Jun 28, 2024

    @benjamingr
    benjamingr
    on Jun 28, 2024
    Member
    More actions

    The PR landed

  18. benjamingr
    closed this as completedon Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.