-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid indexOf() during unmounting a root in the hook #7496
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,23 +39,40 @@ function isNative(fn) { | |
} | ||
} | ||
|
||
var itemMap; | ||
var itemByKey; | ||
|
||
var canUseMap = ( | ||
var canUseCollections = ( | ||
// Array.from | ||
typeof Array.from === 'function' && | ||
// Map | ||
typeof Map === 'function' && | ||
isNative(Map) | ||
isNative(Map) && | ||
// Map.prototype.keys | ||
Map.prototype != null && | ||
typeof Map.prototype.keys === 'function' && | ||
isNative(Map.prototype.keys) && | ||
// Set | ||
typeof Set === 'function' && | ||
isNative(Set) && | ||
// Set.prototype.keys | ||
Set.prototype != null && | ||
typeof Set.prototype.keys === 'function' && | ||
isNative(Set.prototype.keys) | ||
); | ||
|
||
if (canUseMap) { | ||
var itemMap; | ||
var rootIDSet; | ||
|
||
var itemByKey; | ||
var rootByKey; | ||
|
||
if (canUseCollections) { | ||
itemMap = new Map(); | ||
rootIDSet = new Set(); | ||
} else { | ||
itemByKey = {}; | ||
rootByKey = {}; | ||
} | ||
|
||
var unmountedIDs = []; | ||
var rootIDs = []; | ||
|
||
// Use non-numeric keys to prevent V8 performance issues: | ||
// https://github.com/facebook/react/pull/7232 | ||
|
@@ -67,20 +84,21 @@ function getIDFromKey(key) { | |
} | ||
|
||
function get(id) { | ||
if (canUseMap) { | ||
if (canUseCollections) { | ||
return itemMap.get(id); | ||
} else { | ||
var key = getKeyFromID(id); | ||
return itemByKey[key]; | ||
} | ||
var key = getKeyFromID(id); | ||
return itemByKey[key]; | ||
} | ||
|
||
function remove(id) { | ||
if (canUseMap) { | ||
if (canUseCollections) { | ||
itemMap.delete(id); | ||
return; | ||
} else { | ||
var key = getKeyFromID(id); | ||
delete itemByKey[key]; | ||
} | ||
var key = getKeyFromID(id); | ||
delete itemByKey[key]; | ||
} | ||
|
||
function create(id, element, parentID) { | ||
|
@@ -92,12 +110,47 @@ function create(id, element, parentID) { | |
isMounted: false, | ||
updateCount: 0, | ||
}; | ||
if (canUseMap) { | ||
|
||
if (canUseCollections) { | ||
itemMap.set(id, item); | ||
return; | ||
} else { | ||
var key = getKeyFromID(id); | ||
itemByKey[key] = item; | ||
} | ||
} | ||
|
||
function addRoot(id) { | ||
if (canUseCollections) { | ||
rootIDSet.add(id); | ||
} else { | ||
var key = getKeyFromID(id); | ||
rootByKey[key] = true; | ||
} | ||
} | ||
|
||
function removeRoot(id) { | ||
if (canUseCollections) { | ||
rootIDSet.delete(id); | ||
} else { | ||
var key = getKeyFromID(id); | ||
delete rootByKey[key]; | ||
} | ||
} | ||
|
||
function getRegisteredIDs() { | ||
if (canUseCollections) { | ||
return Array.from(itemMap.keys()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I check for Array.from in canUseCollections. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems so (thanks for the tip!) I'll update to check for keys() specifically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad I missed it. It should sufficiently cover the case of |
||
} else { | ||
return Object.keys(itemByKey).map(getIDFromKey); | ||
} | ||
} | ||
|
||
function getRootIDs() { | ||
if (canUseCollections) { | ||
return Array.from(rootIDSet.keys()); | ||
} else { | ||
return Object.keys(rootByKey).map(getIDFromKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we stored the id as the value then this could be Object.values(rootByKey). I suppose we don't know that's polyfilled though. |
||
} | ||
var key = getKeyFromID(id); | ||
itemByKey[key] = item; | ||
} | ||
|
||
function purgeDeep(id) { | ||
|
@@ -208,8 +261,9 @@ var ReactComponentTreeHook = { | |
onMountComponent(id) { | ||
var item = get(id); | ||
item.isMounted = true; | ||
if (item.parentID === 0) { | ||
rootIDs.push(id); | ||
var isRoot = item.parentID === 0; | ||
if (isRoot) { | ||
addRoot(id); | ||
} | ||
}, | ||
|
||
|
@@ -232,11 +286,9 @@ var ReactComponentTreeHook = { | |
// got a chance to mount, but it still gets an unmounting event during | ||
// the error boundary cleanup. | ||
item.isMounted = false; | ||
if (item.parentID === 0) { | ||
var indexInRootIDs = rootIDs.indexOf(id); | ||
if (indexInRootIDs !== -1) { | ||
rootIDs.splice(indexInRootIDs, 1); | ||
} | ||
var isRoot = item.parentID === 0; | ||
if (isRoot) { | ||
removeRoot(id); | ||
} | ||
} | ||
unmountedIDs.push(id); | ||
|
@@ -345,16 +397,9 @@ var ReactComponentTreeHook = { | |
return item ? item.updateCount : 0; | ||
}, | ||
|
||
getRootIDs() { | ||
return rootIDs; | ||
}, | ||
getRegisteredIDs, | ||
|
||
getRegisteredIDs() { | ||
if (canUseMap) { | ||
return Array.from(itemMap.keys()); | ||
} | ||
return Object.keys(itemByKey).map(getIDFromKey); | ||
}, | ||
getRootIDs, | ||
}; | ||
|
||
module.exports = ReactComponentTreeHook; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced early returns with branches because it makes these two paths clearer in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I agree.