secitr 7f6043bdb0 Avoid closure allocation in RCTDeviceEventEmitter.emit when tracing is disabled (#58661)
Summary:
RCTDeviceEventEmitter.emit is one of the hottest paths in React Native:
NativeEventEmitter.emit delegates to it, so every event delivered from
native (scroll, touch, keyboard, app state, ...) goes through this method.
The current implementation wraps every call in Systrace.trace, which
always allocates two closures (a lazy event-name thunk and a callback)
on every emit — even when tracing is disabled, which is the common case.

This changes emit to guard on Systrace.isEnabled() and use
beginEvent/endEvent directly:

- tracing disabled (common case): one branch check + a plain call —
  zero allocations
- tracing enabled: same trace section name (RCTDeviceEventEmitter.emit#<type>)
  and the same begin/end semantics, including endEvent() in a finally
  block when a listener throws

Behavior is unchanged; only the fast-path allocations are removed.

## Changelog:

[General] [Changed] - RCTDeviceEventEmitter.emit no longer allocates closures on every emit when tracing is disabled, reducing per-event allocation on the native-to-JS event path

Pull Request resolved: https://github.com/react/react-native/pull/58661

Test Plan:
- New Fantom integration tests in packages/react-native/Libraries/EventEmitter/__tests__/RCTDeviceEventEmitter-itest.js (public API, runs against Hermes):
  - event + args are forwarded to listeners
  - no trace section calls when tracing is disabled
  - trace section is begun/ended with the correct name when tracing is enabled
  - end section is still emitted when a listener throws
  - __RCTProfileIsProfiling fallback still enables tracing
- yarn test packages/react-native/Libraries — 433 tests pass (30 suites)
- yarn flow-check — 0 errors
- yarn lint — 0 warnings
- Micro-benchmark (Node v24, tracing disabled, 5M emits with a scroll-like payload):
  - before: 158ms total (~31.6 ns/emit)
  - after: 117ms total (~23.4 ns/emit)
  - ~26% faster per emit; on Hermes/mobile CPUs the win comes from avoiding
    two heap-allocated closures per event

Reviewed By: andrewdacenko

Differential Revision: D121799063

Pulled By: cortinico

fbshipit-source-id: 86607670dfe67fa2faf1df130b8b86d828cc2564
2026-09-25 09:05:49 -07:00
2026-09-24 17:03:47 -07:00
2025-07-15 10:10:35 -07:00
2026-09-24 17:03:47 -07:00
2026-01-06 03:23:09 -08:00
2026-06-08 03:32:23 -07:00
2026-06-04 10:12:52 -07:00
2026-02-10 07:41:08 -08:00
2026-09-24 17:03:47 -07:00
2026-09-24 17:03:47 -07:00

React Native logo

Learn once, write anywhere:
Create native apps for Android, iOS, and more using React

React Native is released under the MIT license Current npm package version Monthly npm downloads Follow @reactnative on X

Getting Started · Learn the Basics · Showcase · Contribute · Community · Support

React Native

React Native lets you build native apps using React. Written in JavaScript, rendered with native code.

  • Native UI. React Native primitives render to native platform UI, meaning your app uses the same native platform APIs other apps do. Gestures, text scaling, and accessibility behave the way users expect on each OS.
  • React, everywhere. Declarative UI, components, hooks, and Suspense, reused across Android, iOS, and other platforms.
  • Developer Velocity. See local changes in seconds. Changes to JavaScript code are applied with Fast Refresh, without rebuilding the native app.
  • Extend it yourself. Native Modules let you call platform code directly from JavaScript, synchronously and type-safe — or reach for thousands of existing libraries.

React Native is developed and supported by many companies and individual core contributors. Find out more on the React Foundation website.

Building your first React Native app

Follow the Getting Started guide for a new app, or Integration with Existing Apps to adopt React Native incrementally.

Using a Framework

We believe that the best way to experience React Native is through a Framework, a toolbox with all the necessary APIs to let you build production ready apps. Expo is a production-grade React Native Framework, with file-based routing, a standard library of native modules, and much more.

To create a new Expo project, run the following in your terminal:

npx create-expo-app@latest

Then follow the rest of Expo's getting started guide to start building.

Without a Framework

You can also use React Native without a Framework, however we've found that most developers benefit from one — navigation, native dependencies, and platform tooling are problems the ecosystem has already solved. If a Framework doesn't suit your app, follow Getting Started Without a Framework.

Documentation

The full documentation for React Native can be found on our website.

The source for the React Native docs and website is hosted on a separate repository, react/react-native-website.

Contributing

The main purpose of this repository is to continue evolving React Native core. We want to make contributing to this project as easy and transparent as possible, and we are grateful to the community for contributing bug fixes and improvements. Read below to learn how you can take part in improving React Native.

Code of Conduct

Meta has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.

Contributing Guide

Read our Contributing Guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to React Native.

Discussions

Larger discussions and proposals are discussed in react-native-community/discussions-and-proposals.

React Native releases are discussed in reactwg/react-native-releases.

License

React Native is MIT licensed, as found in the LICENSE file.

Languages
C++ 34.7%
Kotlin 21.8%
JavaScript 19.8%
Objective-C++ 9.7%
Objective-C 5.5%
Other 8.4%