Separate Object.is polyfill (#14334)
* Separate_Object_Is_Polyfill
Maksim Markelov committed
Jan 8, 2019 at 17:21 UTC
a9b035b0c2b8235405835beca0c4db2cc37f18d0
4 files changed
+25
-34
packages/react-reconciler/src/ReactFiberNewContext.js
+2
-8
@@ -26,6 +26,7 @@ import {ContextProvider, ClassComponent} from 'shared/ReactWorkTags';
26
27
import invariant from 'shared/invariant';
28
import warning from 'shared/warning';
29
+import is from 'shared/objectIs';
30
import {
31
createUpdate,
32
enqueueUpdate,
@@ -104,14 +105,7 @@ export function calculateChangedBits<T>(
105
newValue: T,
106
oldValue: T,
107
) {
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)) {
109
// No change
110
return 0;
111
} else {
packages/shared/areHookInputsEqual.js
+2
-8
@@ -8,6 +8,7 @@
8
*/
9
10
import warning from 'shared/warning';
11
+import is from './objectIs';
12
13
export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
14
// Don't bother comparing lengths in prod because these arrays should be
@@ -24,14 +25,7 @@ export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
25
);
26
}
27
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])) {
29
continue;
30
}
31
return false;
packages/shared/objectIs.js
new
+20
@@ -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
@@ -7,27 +7,10 @@
7
* @flow
8
*/
9
10
-/*eslint-disable no-self-compare */
10
+import is from './objectIs';
11
12
const hasOwnProperty = Object.prototype.hasOwnProperty;
13
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
-
14
/**
15
* Performs equality by iterating through keys on an object and returning false
16
* when any key has values which are not strictly equal between the arguments.