Open Biki-das opened 2 months ago
@poteto would love to know your thoughts on what you think about this.
Is the benchmark accounting for objects with varying numbers of keys? The current implementation enables an early exit when objects have different key counts, avoiding the need to iterate through all the keys.
These are not functionally the same.
for-in
includes inherited properties. It needs an additional hasOwnProperty
check on keys of objA
to be comparable to iterating Object.keys(objA)
and at that point might become considerably slower esp. for large objects.
if we look at this file https://github.com/facebook/react/blob/main/packages/shared/shallowEqual.js which is a utility for shallowEqual, in this code we are using Object.keys() method, i tried to have this method working without Object.keys() , which does not create intermediate arrays.
this actually has a near around 30% better performance in the comparison loop we are trying to do, now that can be something that varies from browser to browser, and also due to many other factors, but would love to know the opinion of the maintainers of the same of what they think about, i am attaching a JS benchmark which calculates the benchmark for the both piece of code.
React version: 18.2.0
Steps To Reproduce
The code block 2 contains the piece of code which does not uses Object.keys() and is much faster.
https://github.com/user-attachments/assets/8fbe9850-8f4d-4de4-be15-96bff5db10dd
Current Code
Code that has a better performance