简体   繁体   English

javascript 第一个字节的时间?

[英]Time to first byte with javascript?

Is there any modern browser that via javascript exposes time to first byte (TTFB) and/or time to last byte (TTLB) on a http request without resorting to any plugin?是否有任何现代浏览器通过 javascript 在 http 请求上公开时间到第一个字节 (TTFB) 和/或到最后一个字节 (TTLB) 的时间,而不使用任何插件?

What I would like is a javascript snippet that can access these values and post them back the the server for performance monitoring purposes.我想要的是一个 javascript 片段,它可以访问这些值并将它们发送回服务器以进行性能监控。

Clarification: I am not looking for any js timers or developer tools.澄清:我不是在寻找任何 js 计时器或开发人员工具。 What I wonder and hoping is if there are any browsers that measures load times and exposes those value via javascript.我想知道并希望是否有任何浏览器可以测量加载时间并通过 javascript 公开这些值。

What you want is the W3C's PerformanceTiming interface.您想要的是 W3C 的PerformanceTiming接口。 Browser support is good (see this survey from Sep 2011).浏览器支持很好(参见 2011 年 9 月的调查)。 Like you speculated in response to Shadow Wizard's answer, these times are captured by the browser and exposed to javascript in the window object.就像您在响应影子向导的回答时推测的那样,这些时间被浏览器捕获并暴露给 window object 中的 javascript。 You can find them in window.performance.timing .您可以在window.performance.timing中找到它们。 The endpoint of your TTFB interval will be window.performance.timing.responseStart (defined as "the time immediately after the user agent receives the first byte of the response from the server, or from relevant application caches or from local resources").您的 TTFB 间隔的端点将是window.performance.timing.responseStart (定义为“用户代理从服务器、相关应用程序缓存或本地资源接收到响应的第一个字节之后的时间”)。 There are some options for the starting point, depending on whether you're interested in time to unload the previous document, or time to resolve DNS, so you probably want to read the documentation and decide which one is right for your application.起点有一些选项,取决于您是否有兴趣卸载以前的文档,或有时间解决 DNS,因此您可能需要阅读文档并决定哪一个适合您的应用程序。

I fear it's just not possible.我担心这是不可能的。

JavaScript becomes "active" only after part of the request has been sent from server, accepted by the browser and parsed. JavaScript 只有在从服务器发送部分请求、被浏览器接受并解析后才变为“活动”。

What you ask is kind like asking "Can I measure the weight of a cake after eating it?"你问的问题就像问“我可以在吃完蛋糕后测量它的重量吗?” - you need to first weight and only then eat the cake. - 你需要先称重,然后才能吃蛋糕。

You can see the response time in the Chrome Developer Tools.您可以在 Chrome 开发者工具中查看响应时间。

It's impossible to get the true TTFB in JS, as the page gets a JS context only after the first byte has been received.在 JS 中获得真正的 TTFB 是不可能的,因为页面只有在收到第一个字节后才会获得 JS 上下文。 The closest you can get is with something like the following:您可以获得的最接近的是以下内容:

<script type="text/javascript">var startTime = (new Date()).getTime()</script>

very early in your <head> tag.在您的<head>标记中很早。 Then depending on if you want to check when the html finishes, or everything finishes downloading, you can either put a similar tag near the bottom of your html page (and subtract the values), and then do an XHR back to the server (or set a cookie, which you can retrieve server side on the next page request) or listen to the onload event, and do the same.然后根据您是否要检查 html 何时完成或所有内容完成下载,您可以在 html 页面底部附近放置一个类似的标签(并减去这些值),然后执行 XHR 返回服务器(或设置一个cookie,您可以在下一页请求中检索服务器端)或监听onload事件,并执行相同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM