测试压缩 JavaScript 代码的性能增益
我使用了 5 个 JavaScript 压缩器来压缩 JavaScript 库(JSMin、YUI 压缩器、Packer、闭包编译器和 UglifyJS)
现在我知道闭包编译器是减少文件大小的赢家。 不过,我还想测试一下性能提升。这样做的好方法是什么?
我制作了一个简单的测试页面,它使用了库的所有公共方法。有没有工具可以测试这个测试页面的页面速度?例如。在浏览器上运行 X 次并返回平均加载速度。
感谢您的回答!
I have used 5 JavaScript compressors to compress a JavaScript library (JSMin, YUI compressor, Packer, closure compiler and UglifyJS)
Now I know that closure compiler is the winner in reducing the filesize.
However, I also want to test out the performance gains. What would be a good way to do this?
I made a simple test page that uses all the library's public methods. Is there a tool for testing out the page speed of this test page? Eg. running it X times on a browser and return the average loading speed.
Thanks for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有必要太复杂:
中的脚本通常是串行加载的,因此这应该是测量脚本执行时间的合理方法。如果您有执行
形式的脚本,则在该函数末尾而不是正文末尾进行经过时间的计算。
此方法不会测量通过 setTimeout 或 setInterval 执行的“异步”函数的执行时间,但这些函数不应计入加载时间。
一个替代并且可能更简单的选项是使用 Chrome 或 Safari 的网络检查器内置的 javascript 分析器。
There's no need to be complex about it:
Scripts in the
<head>
generally load serially, so this should be a reasonable method for measuring script execution time. If you have scripts executing form<body onload="...">
, then do the time elapsed calculation at the end of that function instead of the end of the body.This method would not measure execution time for "asynchronous" functions executed via
setTimeout
orsetInterval
, but those shouldn't count against load time.An alternative and likely simpler option is to use the javascript profiler built-in to Chrome or Safari's web inspector.
我怀疑您正在寻找PageSpeed 工具。
I suspect the PageSpeed tool is what you're looking for.
在 Firefox 上使用 PageSpeed 或 YSlow 或在 IE 上使用 HTTPAnaylser 来测试时间负载差异。
Use PageSpeed or YSlow on firefox or HTTPAnaylser on IE to test the time load differences.
这实际上取决于您的受众最关心该网站的什么。是时候出现在屏幕上了?加载完成时间?动画流畅度?交互响应能力?还是原始计算速度?
您应该根据最重要的指标来分析使用不同压缩器压缩的网站。
旁注:简单模式下的闭包编译器仅产生最小的加速。它减小了文件大小,但 JavaScript 程序保持不变。要显着减少代码并优化速度,您必须使用高级模式。
It really depends on what your audience cares most about the site. Time to appear on screen? Time to load-complete? Animation smoothness? Interactive responsiveness? Or raw calculation speed?
You should profile your site compressed with different minifiers based on what the most important metrics are.
Side Note: The Closure Compiler in Simple Mode only yields minimal speedup's. It gets file size down, but the JavaScript program remains the same. To get significant code reduction and speed optimizations, you have to use Advanced Mode.