Join GitHub today
assert: Add support for Map and Set in deepEqual #12142
assert.deepEqual and assert.deepStrictEqual currently return true for
Update: Based on conversation below, the implementation now incurs an O(n^2) cost if the sets or maps are not equal, because to be correct we need to check all pairs of values in sets, and all pairs of keys in a map. This may become exponentially more expensive with input which does deep equality checking on nested maps. But given these checks are almost always only done during testing, and on small objects its probably the right approach.
Update 2: Based on a clever suggestion by @TimothyGu the O(n^2) cost is now only paid when you're either in non-strict mode or when you're using set values & map keys which have typeof object.
Note that this change breaks compatibility with previous versions of
Support had been stalled because the assert API was frozen, but assert was recently
Affected core subsystem(s)
Not sure how important this is to the discussion. There are also some subtle differences in SameValueZero (the set of comparison semantics
assert.equal(NaN, NaN); // AssertionError: NaN === NaN assert.strictEqual(NaN, NaN); // AssertionError: NaN === NaN const map1 = new Map([[NaN, 0]]); const map2 = new Map([[NaN, 0]]); assert.deepEqual(map1, map2); // No error assert.strictDeepEqual(map1, map2); // No error
@joyeecheung I've rewritten
I think its all very readable now - although using ancillary functions makes it more wordy than it was using loop labels. I'm not sure the code is any better for it. But, we're enough opinions deep that I'm getting exhausted from all this churn on such a minor patch. If anyone wants to make more purely stylistic changes I'm happy to add you as a contributor to josephg/node. You're welcome to modify the PR directly.
Otherwise I don't know the process, but I'm happy for it to be merged.
@josephg Sorry if my reviews are disheartening and thanks for sticking with this PR!