javascript window.performance javascript的解释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16808486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:03:33  来源:igfitidea点击:

Explanation of window.performance javascript

javascript

提问by karthick

I am working on detecting the connection speed, so i planned to go with window.performanceobject for duration calculation.

我正在检测连接速度,所以我计划使用window.performance对象进行持续时间计算。

I am little confused with window.performance.timing object is generated based on the whole page load, or based on the last request and response.

我有点困惑 window.performance.timing 对象是基于整个页面加载生成的,还是基于最后一个请求和响应。

For Example:

例如:

I am having 5 server call for web page load, performance.timingobject is generated based on all the 5 server calls or based on the 5th server call(last call).

我有 5 个服务器调用来加载网页,performance.timing对象是基于所有 5 个服务器调用或基于第 5 个服务器调用(最后一次调用)生成的。

sample connection speed calculation for reference

示例连接速度计算供参考

 var bitsLoaded = 100000; //bits total size of all files (5 server call).
 var duration = performance.timing.responseEnd - performance.timing.navigationStart;
 var speedBps = Math.round(bitsLoaded / duration);
 var speedKbps = (speedBps / 1024).toFixed(2);
 var speedMbps = (speedKbps / 1024).toFixed(2); 

Anything not clear ready to explain

任何不清楚的准备解释

Any idea about window.performance

关于 window.performance 的任何想法

回答by Calvin Zhang

performance.timing

性能计时

Not sure if this chart gives you a better understanding of performance.timing.

不确定此图表是否能让您更好地了解 performance.timing。

For your question:

对于您的问题:

I am having 5 server call for web page load, performance.timing object is generated based on all the 5 server calls or based on the 5th server call(last call).

我有 5 个服务器调用来加载网页,performance.timing 对象是基于所有 5 个服务器调用或基于第 5 个服务器调用(最后一次调用)生成的。

The answer is: performance.timingis generated based on all requests and responses(but not including the ajax ones).

答案是:performance.timing基于所有请求和响应(但不包括 ajax 的)生成。

For the sample connection speed calculation script you gave, I guess the below one is better.

对于您提供的示例连接速度计算脚本,我想下面的更好。

var duration = performance.timing.responseEnd - performance.timing.responseStart;

The reason is: the duration from navigationStartto responseEndincludes DNS timing which does not transfer any data from server to client.

原因是:从navigationStartto的持续时间responseEnd包括 DNS 计时,它不会将任何数据从服务器传输到客户端

Please refer to https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.htmlfor the definition of timings.

时序定义请参考https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html

回答by DarrenVortex

performance.timing is NOT based on the last request and response. It measures the delay between navigationStartstage and loadEventStartstage in a page life cycle. As explained here, it's main use is to determine the Page Load Speed.

In my opinion. performance.timing can't really be used for measuring connection speed since it's initiated Afterthe user has received the response and is a javascript object, which runs on the browser. I suggest you simply ping the user and use the round trip time.

performance.timing 不是基于最后的请求和响应。它测量页面生命周期中navigationStart阶段和loadEventStart阶段之间的延迟。正如这里所解释的,它的主要用途是确定页面加载速度。

在我看来。performance.timing 不能真正用于测量连接速度,因为它是用户收到响应启动的,并且是一个 javascript 对象,它在浏览器上运行。我建议您简单地 ping 用户并使用往返时间。