Performance
Performance 接口可以获取当前页面中与性能相关的信息, 它是高级能力 Timing API 的一部分, 同时也融合了 Timeline API, Navigation Timing API, User Timing API, Resource Timing API
该类型的对象可以通过调用只读属性 window.performance
获取
只读属性
navigation: 获取在指定的时间段内发生的操作相关信息, 包含页面的加载刷新、发生了多少次重定向等
timing: 表示包含延迟相关的性能信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24performance.timing;
// {
// "connectStart": 1685528410085,
// "navigationStart": 1685528410083,
// "secureConnectionStart": 0,
// "fetchStart": 1685528410085,
// "domContentLoadedEventStart": 1685528410878,
// "responseStart": 1685528410185,
// "domInteractive": 1685528410417,
// "domainLookupEnd": 1685528410085,
// "responseEnd": 1685528410186,
// "redirectStart": 0,
// "requestStart": 1685528410087,
// "unloadEventEnd": 0,
// "unloadEventStart": 0,
// "domLoading": 1685528410189,
// "domComplete": 1685528410896,
// "domainLookupStart": 1685528410085,
// "loadEventStart": 1685528410897,
// "domContentLoadedEventEnd": 1685528410878,
// "loadEventEnd": 1685528410897,
// "redirectEnd": 0,
// "connectEnd": 1685528410085
// }memory:
非标准属性
, 表示基本内存使用情况的对象timeOrigin: 表示性能测量开始时的时间的高精度时间戳
事件
- resourcetimingbufferfull: 当浏览器的资源时间性能缓冲区已满时触发
1 | performance.onresourcetimingbufferfull = function (evt) { |
方法
p9e.clearMarks()
将给定的 mark 从浏览器的性能输入缓冲区中移除, 如果未指定参数则所有 entryType 值为 mark 的 PerformanceEntry 将从缓冲区中移除
- 参数 name
1 | performance.clearMarks(); |
p9e.clearMeasures()
将给定的 measure 从浏览器的性能输入缓冲区中移除, 如果未指定参数则所有 entryType 值为 measure 的 PerformanceEntry 将从缓冲区移除
- 参数 name
1 | performance.clearMeasures(); |
p9e.clearResourceTimings()
将所有 entryType 值为 resource 的 PerformanceEntry 从缓冲区移除
1 | performance.clearResourceTimings(); |
p9e.getEntries()
基于给定的 filter 返回一个 PerformanceEntry 对象列表, 如果没有符合 filter 条件的返回空数组, 如果未指定参数则返回所有 PerformanceEntry
- 参数
- options: 可选属性, 一个包含键值对的过滤配置项
- name:
PerformanceEntry
的名字 - entryType:
PerformanceEntry
的 entryType, 合法的类型可以从PerformanceEntry.entryType
获取 - initiatorType: 初始化资源的类型, 在
PerformanceResourceTiming.initiatorType
接口中定义
- name:
- options: 可选属性, 一个包含键值对的过滤配置项
1 | // 获取所有的 PerformanceEntry |
p9e.getEntriesByName()
基于给定的 name 和 entryType 返回一个 PerformanceEntry 对象列表, 如果未找到返回空数组
- 参数
- name:
PerformanceEntry
的名字 - type:
PerformanceEntry
的 entryType
- name:
1 | performance.mark('Begin'); |
p9e.getEntriesByType()
基于给定的 entryType 返回一个 PerformanceEntry 对象的列表, 未指定参数返回 TypeError, 未找到返回空数组
- 参数 entryType:
PerformanceEntry
的 entryType
1 | const entries = performance.getEntriesByType('paint'); |
p9e.mark()
通过
performance.getEntries*
方法可以获取到
根据给定的 name, 在浏览器的性能输入缓冲区中创建一个相关的 timestamp, 如果指定的 name 已经存在于 PerformanceTiming
接口则抛出一个 SyntaxError
- 参数 name: 指定标记的名字
标记的 PerformanceEntry 默认包含的属性
- entryType: 默认为 mark
- name: 调用 mark 方法时指定的 name
- startTime: 调用 mark 方法时的时间戳
- duration: 默认为 0(标记没有持续时间)
1 | const dogMark = performance.mark('dog'); |
p9e.measure()
通过
performance.getEntries*
方法可以获取到
在浏览器的指定 start mark 和 end mark 间的性能输入缓冲区中创建一个指定的 timestamp
- 参数
- name: 指定测量的名字
- startMark: 可选属性, 表示测量开始的标记名字
- endMark: 可选属性, 表示测量结束的标记名字
测量的 PerformanceEntry 默认包含的属性
- entryType: 默认为 measure
- name: 调用 measure 方法时指定的 name
- startTime: 调用 measure 方法开始的时间戳
- duration: 测量的持续时间, 通常为结束时间戳减去开始时间戳的差值
1 | performance.mark('my-measure-start-mark'); |
p9e.now()
返回一个表示从性能测量时刻开始经过的毫秒数, 这个时间戳不是高精度的
1 | performance.now(); |
p9e.setResourceTimingBufferSize()
将浏览器的 resource timing 缓冲区的大小设置为 PerformanceEntry 对象的指定数量
p9e.toJSON()
返回 performance 对象的 JSON 对象
1 | performance.toJSON(); |
PerformanceEntry
PerformanceEntry 对象代表了 performance 时间列表中的单个 metric 数据, 每个 performance entry 都可以在应用运行过程中通过手动调用 performance.mark 或 performance.measure 方法创建, 同时, performance entry 在资源加载的时候也会被动生成(例如 img, css, script 等资源)
属性
- name: 只读属性, 表示 PerformanceEntry 的名字
- entryType: 只读属性, 表示上报的 performance metric 的 entryType 类型, 例如 ‘mark’
- frame/navigation: 文档的地址
- resource: 请求的资源的地址
- mark: 当调用
performance.mark()
方法时作为PerformanceEntry
的 name - measure: 当调用
performance.measure()
方法时作为PerformanceEntry
的 name - paint: 其他例如
first-paint
或first-contentful-paint
- startTime: 只读属性, 表示 metric 上报时的时间
- duration: 只读属性, 表示 DOMHighResTimeStamp 事件的耗时
实例方法
- toJSON() 返回 PerformanceEntry 对象的 JSON 格式数据
1 | performance.getEntries()[0].toJSON(); |
PerformanceObserver
PerformanceObserver 用于检测性能度量事件, 在浏览器的性能时间轴记录新的 performance entry 的时候将会被通知
构造函数
使用给定的观察者生成一个新的 PerformanceObserver
实例, 当通过 observe
方法注册的条目类型的性能条目事件被记录下来时, 会调用该观察者回调
参数 callback
观察者的性能事件被记录时将调用构造函数注册的回调, 回调函数有两个参数:- list: 描述性能观察条目列表
- observer: 调用该函数的 PerformanceObserver 对象
1 | const observer = new PerformanceObserver(function (list, observer) { |
- 返回值 PerformanceObserver 对象
静态属性
- supportedEntryTypes: 只读属性, 返回用户代理支持的 PerformanceObserver 接口的属性数组
1 | PerformanceObserver.supportedEntryTypes; |
observer 方法
observer.disconnect()
用于阻止性能观察者接收任何性能条目事件
- 参数 无
- 返回值 undefined
observer.observe()
用于观察传入的参数中指定的性能条目类型的集合, 当记录一个指定类型的性能条目时, 性能检测对象的回调函数将会被调用
- 参数
- options: 一个包含单个键值对的对象, 该键值对的键名为 entryTypes, 如果未传入 options 或 options 实参为空数组会抛出 TypeError
- entryTypes: 一个放字符串的数组, 字符串的有效值取值在性能条目类型中, 如果取值无效则浏览器会自动忽略
- options: 一个包含单个键值对的对象, 该键值对的键名为 entryTypes, 如果未传入 options 或 options 实参为空数组会抛出 TypeError
- 返回值 undefined
1 | observer.observe({ entryTypes: ['element', 'mark', 'measure'] }); |
observer.takeRecords()
返回当前存储在性能观察器中的性能条目列表, 并将其清空
- 参数 无
- 返回值 PerformanceEntry 对象列表
1 | const records = observer.takeRecords(); |