@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
65 lines (63 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.profileSSROperation = void 0;
var _isSsr = require("../core-utils/is-ssr");
var profileSSROperationImpl = function profileSSROperationImpl(segmentName, fn, onSSRMeasure) {
if (!onSSRMeasure) {
return fn();
}
var startTimestamp = performance.now();
try {
return fn();
} finally {
onSSRMeasure({
segmentName: segmentName,
startTimestamp: startTimestamp,
endTimestamp: performance.now()
});
}
};
var profileSSROperationNoOp = function profileSSROperationNoOp(_segmentName, fn, _onSSRMeasure) {
return fn();
};
/**
* Profiles a synchronous operation during Server-Side Rendering (SSR).
*
* This function wraps an operation to measure its execution time in SSR mode.
* On client builds, the profiling is optimized away and the function executes normally.
* On SSR builds, it captures timing data using `performance.now()` and reports it via the callback.
*
* **Important notes:**
* - Profiling only occurs in SSR mode when `onSSRMeasure` is provided
* - Both `startTimestamp` and `endTimestamp` are absolute values from `performance.now()`, not relative times
* - Calculate duration as: `endTimestamp - startTimestamp`
* - The measurement is guaranteed to be reported even if the function throws an error (via `finally` block)
* - On client builds, this function has zero performance overhead
*
* @param segmentName - Identifier for the operation being measured (e.g., 'ssr-app/render/editor/createSchema')
* @param fn - The synchronous function to execute and profile
* @param onSSRMeasure - Optional callback to receive performance measurements
* @param onSSRMeasure.segmentName - Name of the measured segment
* @param onSSRMeasure.startTimestamp - Absolute timestamp when the operation started (from `performance.now()`)
* @param onSSRMeasure.endTimestamp - Absolute timestamp when the operation completed (from `performance.now()`)
*
* @returns The result of the executed function
*
* @example
* // Profile schema creation during SSR
* const schema = profileSSROperation(
* 'ssr-app/render/editor/createSchema',
* () => createSchema(config),
* (measure) => {
* const duration = measure.endTimestamp - measure.startTimestamp;
* console.log(`${measure.segmentName}: ${duration}ms`);
* }
* );
*
* @example
* // Without callback, executes normally
* const result = profileSSROperation('operation', () => expensiveCalculation());
*/
var profileSSROperation = exports.profileSSROperation = (0, _isSsr.isSSR)() ? profileSSROperationImpl : profileSSROperationNoOp;