简体   繁体   English

测试压缩 JavaScript 代码的性能增益

[英]Testing out the performance gain of compressing JavaScript code

I have used 5 JavaScript compressors to compress a JavaScript library (JSMin, YUI compressor, Packer, closure compiler and UglifyJS)我使用了 5 个 JavaScript 压缩器来压缩 JavaScript 库(JSMin、YUI 压缩器、Packer、闭包编译器和 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.在浏览器上运行 X 次并返回平均加载速度。

Thanks for your answers!感谢您的回答!

There's no need to be complex about it:无需复杂:

<html>
<head>
    <script>
    var time = new Date();
    </script>
    <script src="..."></script>
    ... more scripts ... 
</head>

<body>
<script>
    document.write("Time: " + String((new Date() - time)/1000) + " seconds");
</script>
</body>
</html>

Scripts in the <head> generally load serially, so this should be a reasonable method for measuring script execution time. <head>中的脚本通常是串行加载的,因此这应该是衡量脚本执行时间的合理方法。 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.如果您有执行表单的脚本<body onload="..."> ,则在该 function 的末尾而不是正文的末尾进行经过时间的计算。

This method would not measure execution time for "asynchronous" functions executed via setTimeout or setInterval , but those shouldn't count against load time.此方法不会测量通过setTimeoutsetInterval执行的“异步”函数的执行时间,但这些不应计入加载时间。

An alternative and likely simpler option is to use the javascript profiler built-in to Chrome or Safari's web inspector.一种可能更简单的选择是使用 Chrome 或 Safari 的 web 检查器内置的 javascript 分析器。

I suspect the PageSpeed tool is what you're looking for.我怀疑PageSpeed 工具是您正在寻找的。

Use PageSpeed or YSlow on firefox or HTTPAnaylser on IE to test the time load differences.使用 firefox 上的 PageSpeed 或 YSlow 或 IE 上的 HTTPAnaylser 来测试时间负载差异。

It really depends on what your audience cares most about the site.这实际上取决于您的受众最关心该网站的内容。 Time to appear on screen?是时候出现在屏幕上了吗? Time to load-complete?是时候加载完成了吗? Animation smoothness? Animation 平滑度? 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.它减小了文件大小,但 JavaScript 程序保持不变。 To get significant code reduction and speed optimizations, you have to use Advanced Mode.要获得显着的代码缩减和速度优化,您必须使用高级模式。

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

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