Skip to content

Commit

Permalink
[skip ci] Add benchmark for EventTarget (facebook#48886)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [internal]

Creates a benchmarks to measure the performance of `EventTarget`.

Differential Revision: D67750677
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 24, 2025
1 parent a44cd16 commit 7f82efc
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 22 deletions.
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', () => {});
}
}
},
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import '../../../../../../Libraries/Core/InitializeCore.js';

import Event from '../Event';
import EventTarget from '../EventTarget';
import {
EVENT_TARGET_GET_THE_PARENT_KEY,
dispatchTrustedEvent,
} from '../internals/EventTargetInternals';
import {dispatchTrustedEvent} from '../internals/EventTargetInternals';
import createEventTargetHierarchyWithDepth from './createEventTargetHierarchyWithDepth';

let listenerCallOrder = 0;

Expand Down Expand Up @@ -57,24 +55,6 @@ function createListener(
return listener;
}

function createEventTargetHierarchyWithDepth(
depth: number,
): Array<EventTarget> {
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;
}

describe('EventTarget', () => {
describe('addEventListener', () => {
it('should throw an error if event or callback are NOT passed', () => {
Expand Down
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;
}

0 comments on commit 7f82efc

Please sign in to comment.