Skip to content

Commit

Permalink
Fix perf regression introduced in software-mansion#3980 🙃 (software-m…
Browse files Browse the repository at this point in the history
…ansion#4059)

## Summary

This PR replaces implementation of shallowEqual – it turns out that the
new one despite using Object.keys is 20x faster on hermes (haven't
checked JSC). Apparently JIT makes it very hard to measure but the
difference can be easily noticed when running examples with gestures
e.g., snap and drag.

## Test plan

Run snap & drag example on emulator on iOS – with the new implementation
it does not stutter.
  • Loading branch information
kmagiera authored Feb 17, 2023
1 parent d82f9f6 commit a7ea11e
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/reanimated2/hook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,17 @@ export function isAnimated(prop: NestedObjectValues<AnimationObject>): boolean {

export function shallowEqual(a: any, b: any) {
'worklet';
let aKeys = 0;
for (const key in a) {
aKeys += 1;
if (b[key] === a[key]) {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
for (let i = 0; i < aKeys.length; i++) {
if (a[aKeys[i]] !== b[aKeys[i]]) {
return false;
}
}
// we use for loop here, as we want to avoid calling Object.keys that allocates new array
let bKeys = 0;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const key in b) {
bKeys += 1;
}
return aKeys === bKeys;
return true;
}

export const validateAnimatedStyles = (styles: AnimatedStyle): void => {
Expand Down

0 comments on commit a7ea11e

Please sign in to comment.