deep-eql icon indicating copy to clipboard operation
deep-eql copied to clipboard

objectEqual Sorts Keys Before Compare

Open xee5ch opened this issue 6 years ago • 2 comments

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.

xee5ch avatar Jul 14 '18 02:07 xee5ch

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!

keithamus avatar Jul 14 '18 14:07 keithamus

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.

xee5ch avatar Jul 14 '18 16:07 xee5ch