Skip to content

Commit 158aa38

Browse files
author
Your Name
committed
changes
1 parent a55841c commit 158aa38

5 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/**\n * Best Practices for Using Differ Functions\n * \n * 1. Remember the Return Convention\n * ❌ if (deepDiffer(a, b)) { /* a == b */ }\n * ✅ if (deepDiffer(a, b)) { /* a != b */ }\n * \n * 2. Use Specialized Differs When Possible\n * Prefer pointsDiffer over deepDiffer for points\n * Prefer sizesDiffer over deepDiffer for sizes\n * General rule: use most specific function for your data type\n * \n * 3. Be Careful with Max Depth\n * deepDiffer({a: {b: {c: 1}}}, {a: {b: {c: 2}}}, 2)\n * This won't detect the difference at c because depth is limited\n * \n * 4. Function Comparison Behavior\n * deepDiffer treats all functions as equal by default\n * This is intentional - function identity shouldn't trigger updates\n * \n * 5. Null/Undefined Handling\n * Most differs handle null/undefined gracefully\n * They use dummy objects to avoid crashes\n * \n * 6. Performance Notes\n * - Use differs in shouldComponentUpdate/useMemo\n * - Avoid deep nesting - consider flattening your data\n * - Array differences always trigger full array check\n * - Early exit on first difference found\n * \n * 7. Common Pitfalls\n * ❌ deepDiffer(prev, curr) === false means no change (WRONG)\n * ✅ deepDiffer(prev, curr) === false means no change (CORRECT)\n * ✅ !deepDiffer(prev, curr) means no change (CORRECT)\n */\n
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Differ Utilities Module Documentation
3+
*
4+
* Purpose:
5+
* --------
6+
* Provides optimized equality comparison functions for React Native styling
7+
* and layout calculations. These are specialized differ functions designed for
8+
* high-performance use in the render loop where speed is critical.
9+
*
10+
* Return Value Convention:
11+
* -----------------------
12+
* All differ functions return:
13+
* - true = values are DIFFERENT
14+
* - false = values are EQUAL
15+
*
16+
* This is opposite to standard JS equality (===) and is optimized for
17+
* React's shouldComponentUpdate pattern.
18+
*
19+
* Available Functions:
20+
* -------------------
21+
*
22+
* deepDiffer(one, two, maxDepth?, options?)
23+
* Deep recursive comparison for arbitrary objects/arrays
24+
* - O(n) worst case, but short-circuits on first difference
25+
* - Supports max depth limiting to avoid excessive recursion
26+
* - Handles circular references gracefully
27+
*
28+
* matricesDiffer(one, two)
29+
* Optimized for 4x4 transformation matrices
30+
* - O(1) fixed size, checks indices by change likelihood
31+
* - Critical for transform performance
32+
*
33+
* pointsDiffer(one, two)
34+
* Compares {x, y} coordinate pairs
35+
* - O(1) tiny allocation footprint
36+
* - Null-safe with dummy object fallback
37+
*
38+
* sizesDiffer(one, two)
39+
* Compares {width, height} dimensions
40+
* - O(1) with graceful null handling
41+
* - Commonly used in layout calculations
42+
*
43+
* insetsDiffer(one, two)
44+
* Compares {top, left, right, bottom} spacing
45+
* - O(1) for padding/margin comparisons
46+
* - Null-safe with dummy fallback
47+
*
48+
* Performance Characteristics:
49+
* ---------------------------
50+
* All functions are allocation-free after initialization and suitable for
51+
* use in hot paths like render methods and event handlers.
52+
*/

packages/react-native/Libraries/Utilities/differ/deepDiffer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,25 @@ function deepDiffer(
112112

113113
deepDiffer.unstable_setLogListeners = unstable_setLogListeners;
114114
export default deepDiffer;
115+
116+
/**
117+
* USAGE EXAMPLES:
118+
*
119+
* // Primitive comparison
120+
* deepDiffer(1, 2); // true
121+
* deepDiffer('hello', 'hello'); // false
122+
*
123+
* // Object comparison
124+
* deepDiffer({a: 1}, {a: 1}); // false
125+
* deepDiffer({a: 1, b: 2}, {a: 1}); // true
126+
*
127+
* // Array comparison
128+
* deepDiffer([1, 2], [1, 2]); // false
129+
* deepDiffer([1, 2], [1, 3]); // true
130+
*
131+
* // Nested structures
132+
* deepDiffer({a: {b: [1, 2]}}, {a: {b: [1, 2]}}); // false
133+
*
134+
* // With depth limit
135+
* deepDiffer({a: {b: {c: 1}}}, {a: {b: {c: 2}}}, 2); // false (depth limited)
136+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
/**
12+
* Collection of specialized comparison functions for React Native styling.
13+
*
14+
* These utilities provide optimized equality checks for common data types
15+
* used in styling and layout calculations. They return true if values differ,
16+
* false if equal - opposite of standard equality comparisons.
17+
*/
18+
19+
export {default as deepDiffer} from './deepDiffer';
20+
export {default as matricesDiffer} from './matricesDiffer';
21+
export {default as pointsDiffer} from './pointsDiffer';
22+
export {default as sizesDiffer} from './sizesDiffer';
23+
export {default as insetsDiffer} from './insetsDiffer';

packages/react-native/Libraries/Utilities/differ/pointsDiffer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const dummyPoint: Point = {x: undefined, y: undefined};
3030
function pointsDiffer(one: ?Point, two: ?Point): boolean {
3131
one = one || dummyPoint;
3232
two = two || dummyPoint;
33+
// Null-safe comparison: handles undefined values gracefully
3334
return one !== two && (one.x !== two.x || one.y !== two.y);
3435
}
3536

0 commit comments

Comments
 (0)