Skip to content

Commit

Permalink
Refactor: Minor performance improvement of BackHandler.removeEventLis…
Browse files Browse the repository at this point in the history
…tener (#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: #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
  • Loading branch information
vitalii-tb authored and facebook-github-bot committed Jul 27, 2022
1 parent 98d74d6 commit 14c207d
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Libraries/Utilities/BackHandler.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
};
Expand Down

0 comments on commit 14c207d

Please sign in to comment.