lynx: performance readonly object

通过该对象访问当前页面与性能指标有关的信息。

实例方法

createObserver()

创建一个 PerformanceObserver 对象,该对象将用于接收 Lynx engine 产生的性能事件。

语法

createObserver(callback: PerformanceCallback): PerformanceObserver;

参数

callback

处理性能事件的回调函数,类型定义为 type PerformanceCallback = (entry: PerformanceEntry) => void。entry 是 PerformanceEntry 的子类型。

PerformanceObserver 接收到性能事件时该回调被触发。

返回值

一个 PerformanceObserver 对象。

示例

该示例展示了如何创建一个 PerformanceObserver 并监听 metric.fcppipeline 事件。

profileStart()

标记一个 trace event 的开始。

语法

profileStart(name: string, option?: { flowId: number, arg?:  Record<string, string>}): void;

参数

name

trace event 的名称。

option(可选)

额外选项,例如 flowId 和自定义参数(arg)。

示例

// 开始一个 trace event。
lynx.performance.profileStart('DataFetch');

// 开始一个 trace event 并添加自定义参数
lynx.performance.profileStart('DataFetch', {
  args: { url: 'https://api.example.com/data', method: 'GET' },
});

// 开始一个 flow 类型的 trace event
const flowId = lynx.performance.profileFlowId();
lynx.performance.profileStart('UserAction', { flowId });

// 开始一个 flow 类型的 trace event 并添加自定义参数
lynx.performance.profileStart('ComplexProcess', {
  args: { step: 'start' },
  flowId,
});

profileEnd()

标记最近的 trace event 的结束。

语法

profileEnd(): void;

示例

// 结束最近的 trace event
lynx.performance.profileEnd();

profileMark()

标记一个即时(没有持续时间) trace event。

语法

profileMark(name: string, option?: { flowId: number, arg?:  Record<string, string>}): void;

参数

name

trace event 的名称。

option(optional)

额外选项,如 flowId 和自定义参数(arg)。

示例

// 标记一个即时 trace event
lynx.performance.profileMark('FetchStarted');

// 标记一个即时 trace event 并添加自定义参数
lynx.performance.profileMark('FetchStarted', {
  args: { status: 'initiated' },
});

// 标记一个 flow 类型的即时 trace event
const flowId = lynx.performance.profileFlowId();
lynx.performance.profileMark('ActionCheckpoint', { flowId });

// 标记一个 flow 类型的即时 trace event 并添加自定义参数
lynx.performance.profileMark('ProcessMidpoint', {
  args: { step: 'middle' },
  flowId,
});

profileFlowId()

flow events 生成一个唯一的 flow id.

语法

profileFlowId(): number;

返回值

一个唯一的 flow id 数字。

示例

const flowId = lynx.performance.profileFlowId();
lynx.performance.profileStart('UserAction', { flowId });
lynx.performance.profileMark('ActionCheckpoint', { flowId });
lynx.performance.profileEnd();

isProfileRecording()

检查当前是否正在进行 trace 录制。

语法

isProfileRecording(): boolean

返回值

如果正在录制 trace,返回 true

Examples

if (lynx.performance.isProfileRecording()) {
  lynx.performance.profileMark('RecordingIsActive');
}

兼容性

LCD tables only load in the browser

除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。