使用javascript到第一个字节的时间? [英] Time to first byte with javascript?

查看:84
本文介绍了使用javascript到第一个字节的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何现代浏览器通过javascript在http请求中公开时间到第一个字节(TTFB)和/或时间到最后一个字节(TTLB)而不诉诸任何插件?

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片段,它可以访问这些值并将它们发回服务器以进行性能监控。

What I would like is a javascript snippet that can access these values and post them back the the server for performance monitoring purposes.

澄清:
我不是在寻找任何js计时器或开发人员工具。我想知道并希望是否有任何浏览器可以测量加载时间并通过javascript公开这些值。

Clarification: I am not looking for any js timers or developer tools. What I wonder and hoping is if there are any browsers that measures load times and exposes those value via javascript.

推荐答案

你想要什么是W3C的 PerformanceTiming 界面。浏览器支持很好(参见 2011年9月的这项调查。就像你在回应Shadow Wizard的答案时推测的那样,这些时间被浏览器捕获并暴露在window对象中的javascript中。您可以在 window.performance.timing 中找到它们。您的TTFB间隔的端点将是 window.performance.timing.responseStart (定义为用户代理从服务器收到响应的第一个字节后的时间,或者来自相关的应用程序缓存或来自本地资源)。根据您是否有时间卸载以前的文档,或者是时候解析DNS,有一些选项可供选择,因此您可能需要阅读文档并确定哪一个适合您的应用程序。

What you want is the W3C's PerformanceTiming interface. Browser support is good (see this survey from Sep 2011). 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. You can find them in 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"). 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.

这篇关于使用javascript到第一个字节的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆