-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set and Map performance and correctness #46
Comments
Thanks for the issue @BridgeAR. Definitely looks like you've uncovered some problems here. Chai is a 100% community-driven project; we'd love a PR if you can carve out the time! |
I can not promise that I have time for that anytime soon. I have a couple of things that I am working on at the moment. |
Writing tests for the project I'm working on, I found: deepEql( new Set([new Set([1,2]), new Set([3,4])]) ,
new Set([new Set([4,3]), new Set([2,1])]) ); // returns false So I had to switch to Node's |
@escKeyStroke you want that to return |
deepEql(new Set([1, 2]), new Set([2, 1])) // passes This is highly inconsistent to the object version and about Sets being ordered - it is true that it is possible to iterate over the elements in insertion order but due to the nature of |
This should be fixed then. I feel like we have had this discussion before but I'd challenge that sets should only equal with the same order. Can you give a use-case for why you'd want to compare two unordered sets? |
I personally do not have a use case at hand as I personally never needed to compare sets or maps. But in general I can imagine that people just want to collect data in a set / map and compare this with predefined values in a test and I would argue they are equal, no matter the order of the objects. |
Take my bicycle, disassemble it and put the pieces in a box. When I (or the app I'm writing, or whatever) see inside the box, I will recognize my bike no matter the order in which you put the pieces inside. I am using A discussion you might want to participate in: |
Okay, I am enough convinced. However this may be derailing the original issue. Let's focus on fixing the original issue and hopefully fix the other ones alongside. |
One of the issues we may have here to do with performance - is that IE11 has no way to iterate on sets other than |
It's a judgement call but I agree that this module shouldn't consider insertion order when comparing deep equality of |
Just wanted to add that javascript objects never had iteration order defined by the spec which proved to cause a lot of bugs. This is why they specified iteration order for set and map - to prevent this conversation. Although maps and sets have an iteration order, they are not ordered themselves and cannot be re-ordered without removing and re-inserting values. I feel it would be very unexpected to compare values by insertion order for these constructs. |
@keithamus about performance concerns: this is indeed bad. I would probably do a feature detection in this case. That should be easy. Another way to improve the performance for For the fast path you can pretty much use my implementation as it is in Node.js. Only the loose equal comparison should be removed in that case (and that makes the implementation much easier). With that implementation the complexity for object cases is lower. Primitive cases are actually |
Am I missing something, or are Sets always unequal, regardless of order?
|
I was curious how well chai would handle big sets and maps and checked a couple of things and also stumbled across this PR and I am sorry to say that these benchmarks are somewhat flawed.
The Node.js
assert
module was never used for comparison because the benchmark required a in-existing function that would be replaced withdeep-eql
in that case. The result is that the node and deep-eql results are actually both from the same library so they should be pretty much identical. However, that is not always the case and they partially differ by a big margin e.g.My main concern with these benchmarks is though, that only very simply objects are used. Having nice numbers for those is awesome but they are normally not an issue. If a complex object is used though, this can be really bad.
This time compared to Node.js 8.4 (this time for real)
So Node.js is more then 350 times as fast in this case.
While checking Sets and Maps further I also noticed the following:
When using Sets twice the work is done that is needed in general because the
key
and thevalue
are both the value when returned in aforEach
.Worse though: comparing Sets or Maps with Objects does not even work properly!
Sorting an array with objects is not possible and that is why this bug exists.
I suggest to have a look at a PR from me that would improve the situation and fix the bug as well nodejs/node#14258.
As chai currently does not have a loose equal comparison the algorithm should be pretty straight forward (otherwise there is quite some extra handling for that).
The text was updated successfully, but these errors were encountered: