1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
---
title: Performance.measure()
slug: Web/API/Performance/measure
tags:
- Performance API
- 网页性能
translation_of: Web/API/Performance/measure
---
<div>{{APIRef("User Timing API")}}</div>
<p> <strong><code>measure()</code></strong> 方法在浏览器性能记录缓存中创建了一个名为{{domxref("DOMHighResTimeStamp","时间戳")}}的记录来记录两个特殊标志位(通常称为开始标志和结束标志)。 被命名的{{domxref("DOMHighResTimeStamp","时间戳")}}称为一次测量(measure)。</p>
<p>{{AvailableInWorkers}}</p>
<p>The <code>measure</code> 可以被{{domxref("Performance")}} 接口 <code>getEntries*()</code> 中的方法检查到({{domxref("Performance.getEntries","getEntries()")}}, {{domxref("Performance.getEntriesByName","getEntriesByName()")}} 或者 {{domxref("Performance.getEntriesByType","getEntriesByType()")}}).</p>
<p>The <code>measure's</code> {{domxref("PerformanceEntry","performance entry")}} will have the following property values:</p>
<ul>
<li>{{domxref("PerformanceEntry.entryType","entryType")}} - set to "<code>measure</code>".</li>
<li>{{domxref("PerformanceEntry.name","name")}} - set to the "<code>name</code>" given when the measure was created.</li>
<li>{{domxref("PerformanceEntry.startTime","startTime")}} - set to the start mark {{domxref("DOMHighResTimeStamp","timestamp")}}.</li>
<li>{{domxref("PerformanceEntry.duration","duration")}} - set to a {{domxref("DOMHighResTimeStamp")}} that is the duration of the measure (typically, the end mark timestamp minus the start mark timestamp).</li>
</ul>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><em>performance</em>.measure(name, startMark, endMark);
</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>name</dt>
<dd>一个 {{domxref("DOMString")}}, 代表测量的名字。</dd>
<dt>startMark {{optional_inline}}</dt>
<dd>一个 {{domxref("DOMString")}}, 代表测量的开始标志名字。 May also be the name of a {{domxref("PerformanceTiming")}} property.</dd>
<dt>endMark {{optional_inline}}</dt>
<dd>一个{{domxref("DOMString")}}, 代表测量的结束标志名字。May also be the name of a {{domxref("PerformanceTiming")}} property.</dd>
</dl>
<h3 id="返回值">返回值</h3>
<dl>
<dt>void</dt>
<dd> </dd>
</dl>
<h2 id="例子">例子</h2>
<p>以下例子展示如何在浏览器性能记录缓存中使用 <code>measure()</code>创建一个新的测量记录{{domxref("PerformanceEntry","performance entry")}} 。</p>
<pre class="brush: js">// 以一个标志开始。
performance.mark("mySetTimeout-start");
// 等待一些时间。
setTimeout(function() {
// 标志时间的结束。
performance.mark("mySetTimeout-end");
// 测量两个不同的标志。
performance.measure(
"mySetTimeout",
"mySetTimeout-start",
"mySetTimeout-end"
);
// 获取所有的测量输出。
// 在这个例子中只有一个。
var measures = performance.getEntriesByName("mySetTimeout");
var measure = measures[0];
console.log("setTimeout milliseconds:", measure.duration)
// 清除存储的标志位
performance.clearMarks();
performance.clearMeasures();
}, 1000);
</pre>
<h2 id="详细说明">详细说明</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">详细说明</th>
<th scope="col">状态</th>
<th scope="col">评论</th>
</tr>
<tr>
<td>{{SpecName('User Timing Level 2', '#dom-performance-measure', 'measure()')}}</td>
<td>{{Spec2('User Timing Level 2')}}</td>
<td>Clarifies <code>measure()</code> processing model.</td>
</tr>
<tr>
<td>{{SpecName('User Timing', '#dom-performance-measure', 'measure()')}}</td>
<td>{{Spec2('User Timing')}}</td>
<td>Basic definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.Performance.measure")}}</p>
</div>
|