forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark for EventTarget (facebook#48886)
Summary: Changelog: [internal] Creates a benchmarks to measure the performance of `EventTarget`. Differential Revision: D67750677
- Loading branch information
1 parent
7f527f0
commit 5eff8a5
Showing
3 changed files
with
150 additions
and
22 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...ages/react-native/src/private/webapis/dom/events/__tests__/EventTarget-benchmark-itest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import '../../../../../../Libraries/Core/InitializeCore.js'; | ||
|
||
import Event from '../Event'; | ||
import EventTarget from '../EventTarget'; | ||
import createEventTargetHierarchyWithDepth from './createEventTargetHierarchyWithDepth'; | ||
import {unstable_benchmark} from '@react-native/fantom'; | ||
|
||
let event: Event; | ||
let eventTarget: EventTarget; | ||
|
||
unstable_benchmark | ||
.suite('EventTarget') | ||
.add( | ||
'dispatchEvent, no bubbling, no listeners', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom'); | ||
eventTarget = new EventTarget(); | ||
}, | ||
}, | ||
) | ||
.add( | ||
'dispatchEvent, no bubbling, single listener', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom'); | ||
eventTarget = new EventTarget(); | ||
eventTarget.addEventListener('custom', () => {}); | ||
}, | ||
}, | ||
) | ||
.add( | ||
'dispatchEvent, no bubbling, multiple listeners', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom'); | ||
eventTarget = new EventTarget(); | ||
for (let i = 0; i < 100; i++) { | ||
eventTarget.addEventListener('custom', () => {}); | ||
} | ||
}, | ||
}, | ||
) | ||
.add( | ||
'dispatchEvent, bubbling, no listeners', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom', {bubbles: true}); | ||
const targets = createEventTargetHierarchyWithDepth(100); | ||
eventTarget = targets[targets.length - 1]; | ||
}, | ||
}, | ||
) | ||
.add( | ||
'dispatchEvent, bubbling, single listener per target', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom', {bubbles: true}); | ||
const targets = createEventTargetHierarchyWithDepth(100); | ||
eventTarget = targets[targets.length - 1]; | ||
for (const target of targets) { | ||
target.addEventListener('custom', () => {}); | ||
} | ||
}, | ||
}, | ||
) | ||
.add( | ||
'dispatchEvent, bubbling, multiple listeners per target', | ||
() => { | ||
eventTarget.dispatchEvent(event); | ||
}, | ||
{ | ||
beforeAll: () => { | ||
event = new Event('custom', {bubbles: true}); | ||
const targets = createEventTargetHierarchyWithDepth(100); | ||
eventTarget = targets[targets.length - 1]; | ||
for (const target of targets) { | ||
for (let i = 0; i < 100; i++) { | ||
target.addEventListener('custom', () => {}); | ||
} | ||
} | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ct-native/src/private/webapis/dom/events/__tests__/createEventTargetHierarchyWithDepth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import EventTarget from '../EventTarget'; | ||
import {EVENT_TARGET_GET_THE_PARENT_KEY} from '../internals/EventTargetInternals'; | ||
|
||
/** | ||
* Returns a tree of EventTargets with the given depth (starting from the root). | ||
*/ | ||
export default function createEventTargetHierarchyWithDepth( | ||
depth: number, | ||
): Array<EventTarget> { | ||
if (depth < 1) { | ||
throw new Error('Depth must be greater or equal to 1'); | ||
} | ||
|
||
const targets = []; | ||
|
||
for (let i = 0; i < depth; i++) { | ||
const target = new EventTarget(); | ||
const parentTarget = targets[targets.length - 1]; | ||
|
||
// $FlowExpectedError[prop-missing] | ||
target[EVENT_TARGET_GET_THE_PARENT_KEY] = () => parentTarget; | ||
|
||
targets.push(target); | ||
} | ||
|
||
return targets; | ||
} |