packages/react-native/ReactCommon/react/timing/__docs__/README.md
This directory contains the shared C++ implementation of high-resolution timing primitives for React Native. These primitives provide precise time measurement capabilities that align with Web standards while being optimized for the React Native environment.
The timing primitives in this module are primarily used by internal React Native systems that require precise time measurements. The monotonic clock is used for these primitives: they should be used for measuring time intervals. These primitives should not be used for wall times. They are not expected to be used directly by application developers but serve as foundational components for various React Native features.
Key primitives include:
HighResTimeStamp: A class representing a specific point in time with high
precision.HighResDuration: A class representing a duration of time with high
precision.These primitives support various operations:
// Getting the current high-resolution timestamp
auto start = HighResTimeStamp::now();
// Creating durations
auto duration = HighResDuration::fromNanoseconds(100);
auto durationMs = HighResDuration::fromMilliseconds(100);
// Arithmetic operations
auto later = start + duration;
auto elapsed = later - start;
// Converting to absolute time units of highest precision
auto end = HighResTimeStamp::now();
int64_t nanoseconds = (end - start).toNanoseconds();
// Converting to DOMHighResTimeStamp (for JavaScript interoperability)
double jsTimeValue = now.toDOMHighResTimeStamp();
The timing primitives are designed to align with Web standards while leveraging
C++'s type system and the performance characteristics of native code. The
implementation uses std::chrono internally but provides a more specialized
interface tailored to React Native's needs.
This class represents a specific point in time with high precision. It
encapsulates a std::chrono::steady_clock::time_point and provides methods to:
DOMHighResTimeStamp for JavaScript interoperability.This class represents a duration of time with high precision. It encapsulates a
std::chrono::duration and provides methods to:
DOMHighResTimeStamp for JavaScript interoperability.PerformanceObserver entries (e.g., longtask and
event).