Skip to content
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

Merged
merged 2 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 68 additions & 34 deletions src/isomorphic/hooks/ReactComponentTreeHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,29 @@ function isNative(fn) {
}
}

var itemMap;
var itemByKey;

var canUseMap = (
var canUseCollections = (
typeof Array.from === 'function' &&
typeof Map === 'function' &&
isNative(Map)
typeof Set === 'function' &&
isNative(Map) &&
isNative(Set)
);

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
Expand All @@ -67,20 +73,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 {
Copy link
Collaborator Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree.

var key = getKeyFromID(id);
delete itemByKey[key];
}
var key = getKeyFromID(id);
delete itemByKey[key];
}

function create(id, element, parentID) {
Expand All @@ -92,12 +99,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());
Copy link
Contributor

@syranide syranide Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.keys() is not yet supported in all implementations, IE supposedly does not support this. EDIT: Array.from is even less widely supported.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check for Array.from in canUseCollections.
Do you mean that a native Map may exist without Map.prototype.keys?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@syranide syranide Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad I missed it. It should sufficiently cover the case of .keys() I imagine 👍 EDIT: Yep, .keys() is not part of "basic support".

} else {
return Object.keys(itemByKey).map(getIDFromKey);
}
}

function getRootIDs() {
if (canUseCollections) {
return Array.from(rootIDSet.keys());
} else {
return Object.keys(rootByKey).map(getIDFromKey);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Expand Down Expand Up @@ -208,8 +250,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);
}
},

Expand All @@ -232,11 +275,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);
Expand Down Expand Up @@ -345,16 +386,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;
Original file line number Diff line number Diff line change
Expand Up @@ -1870,15 +1870,18 @@ describe('ReactComponentTreeHook', () => {
});
});

describe('in environment without Map and Array.from', () => {
describe('in environment without Map, Set and Array.from', () => {
var realMap;
var realSet;
var realArrayFrom;

beforeEach(() => {
realMap = global.Map;
realSet = global.Set;
realArrayFrom = Array.from;

global.Map = undefined;
global.Set = undefined;
Array.from = undefined;

jest.resetModuleRegistry();
Expand All @@ -1893,6 +1896,7 @@ describe('ReactComponentTreeHook', () => {

afterEach(() => {
global.Map = realMap;
global.Set = realSet;
Array.from = realArrayFrom;
});

Expand Down