Skip to content

Commit a9b035b

Browse files
mmarkelovtrueadm
authored andcommitted
Separate Object.is polyfill (#14334)
* Separate_Object_Is_Polyfill
1 parent 547e059 commit a9b035b

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

packages/react-reconciler/src/ReactFiberNewContext.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {ContextProvider, ClassComponent} from 'shared/ReactWorkTags';
2626

2727
import invariant from 'shared/invariant';
2828
import warning from 'shared/warning';
29+
import is from 'shared/objectIs';
2930
import {
3031
createUpdate,
3132
enqueueUpdate,
@@ -104,14 +105,7 @@ export function calculateChangedBits<T>(
104105
newValue: T,
105106
oldValue: T,
106107
) {
107-
// Use Object.is to compare the new context value to the old value. Inlined
108-
// Object.is polyfill.
109-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
110-
if (
111-
(oldValue === newValue &&
112-
(oldValue !== 0 || 1 / oldValue === 1 / (newValue: any))) ||
113-
(oldValue !== oldValue && newValue !== newValue) // eslint-disable-line no-self-compare
114-
) {
108+
if (is(oldValue, newValue)) {
115109
// No change
116110
return 0;
117111
} else {

packages/shared/areHookInputsEqual.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import warning from 'shared/warning';
11+
import is from './objectIs';
1112

1213
export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
1314
// Don't bother comparing lengths in prod because these arrays should be
@@ -24,14 +25,7 @@ export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
2425
);
2526
}
2627
for (let i = 0; i < arr1.length; i++) {
27-
// Inlined Object.is polyfill.
28-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
29-
const val1 = arr1[i];
30-
const val2 = arr2[i];
31-
if (
32-
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
33-
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
34-
) {
28+
if (is(arr1[i], arr2[i])) {
3529
continue;
3630
}
3731
return false;

packages/shared/objectIs.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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
8+
*/
9+
10+
/**
11+
* inlined Object.is polyfill to avoid requiring consumers ship their own
12+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
13+
*/
14+
function is(x: any, y: any) {
15+
return (
16+
(x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
17+
);
18+
}
19+
20+
export default is;

packages/shared/shallowEqual.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,10 @@
77
* @flow
88
*/
99

10-
/*eslint-disable no-self-compare */
10+
import is from './objectIs';
1111

1212
const hasOwnProperty = Object.prototype.hasOwnProperty;
1313

14-
/**
15-
* inlined Object.is polyfill to avoid requiring consumers ship their own
16-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
17-
*/
18-
function is(x, y) {
19-
// SameValue algorithm
20-
if (x === y) {
21-
// Steps 1-5, 7-10
22-
// Steps 6.b-6.e: +0 != -0
23-
// Added the nonzero y check to make Flow happy, but it is redundant
24-
return x !== 0 || y !== 0 || 1 / x === 1 / y;
25-
} else {
26-
// Step 6.a: NaN == NaN
27-
return x !== x && y !== y;
28-
}
29-
}
30-
3114
/**
3215
* Performs equality by iterating through keys on an object and returning false
3316
* when any key has values which are not strictly equal between the arguments.

0 commit comments

Comments
 (0)