chaijs / deep-eql

Improved deep equality testing for Node.js and the browser.
MIT License
108 stars 39 forks source link

objectEqual Sorts Keys Before Compare #55

Open xee5ch opened 6 years ago

xee5ch commented 6 years ago

Today, when reserving the objectEqual function in this library while evaluating deep.equal call stacks in a chai test harness, I observed that when comparing objectEqual() from the default branch in extensiveDeepEqualByType) it sorts the keys.

https://github.com/chaijs/deep-eql/blob/04d6da6518f8ddc288638ca42503752028810120/index.js#L418

https://github.com/chaijs/deep-eql/blob/04d6da6518f8ddc288638ca42503752028810120/index.js#L419

Is it not more performant for large objects to just iterate over unsorted properties with something like hasOwnProperty on the rightHandProperty properties? If so, I can make a PR.

keithamus commented 6 years ago

Hey @xee5ch thanks for the issue!

Is it not more performant for large objects to just iterate over unsorted properties

Great question! I don't know the answer to that. We have a benchmark suite already set up so you could have a go at running the benchmarks, modifying the code and rerunning the benchmarks. I'd be interested to see what happens! And we'd welcome a PR to make things faster!

xee5ch commented 6 years ago

Ok let me review the documentation about running the benchmark suite. I was talking with that friend when stepping through the functions called via chai and I presume it does not make a large difference for small objects, but large object comparison could be slow if you sort the keys of both objects.

I will work on a test case, then post back with samples, the benchmark difference, and of course the code change required.