mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Refactor: Minor performance improvement of BackHandler.removeEventListener (#34281)
Summary: I've noticed that `BackHandler.removeEventListener()` performs two same `indexOf()` calls on an array that is not changing. By removing extra `indexOf` we can slightly improve time complexity of `BackHandler.removeEventListener()` from O(2n) to O(n) ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Android] [Fixed] - Remove extra indexOf call in BackHandler.removeEventListener Pull Request resolved: https://github.com/facebook/react-native/pull/34281 Test Plan: 1. Add the following code to any function component ```javascript BackHandler.addEventListener('hardwareBackPress', () => true).remove(); ``` 2. Press on hardware back button Expected result: Application closes Reviewed By: dmitryrykun Differential Revision: D38198510 Pulled By: javache fbshipit-source-id: eab6a57689a536623138a4b3ebddbf9ba87d281f
This commit is contained in:
committed by
Facebook GitHub Bot
parent
98d74d6eb9
commit
14c207d9e1
@@ -97,11 +97,9 @@ const BackHandler: TBackHandler = {
|
||||
eventName: BackPressEventName,
|
||||
handler: () => ?boolean,
|
||||
): void {
|
||||
if (_backPressSubscriptions.indexOf(handler) !== -1) {
|
||||
_backPressSubscriptions.splice(
|
||||
_backPressSubscriptions.indexOf(handler),
|
||||
1,
|
||||
);
|
||||
const index = _backPressSubscriptions.indexOf(handler);
|
||||
if (index !== -1) {
|
||||
_backPressSubscriptions.splice(index, 1);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user