简体   繁体   English

CSS / JS动态缩小? (性能)

[英]CSS/JS On-the-fly Minification? (Performance)

I'm working off the List of Google Pagespeed's guidelines for speeding up pageload times. 我正在研究Google Pagespeed的有关加快页面加载时间的指南列表。 One of them is minifying CSS and JS files. 其中之一是缩小CSS和JS文件。

Since these files change quite often I was thinking of using a PHP script that minifies this script on-the-fly upon request (from the browser). 由于这些文件经常更改,因此我考虑使用一个PHP脚本,该脚本可根据请求(从浏览器)即时缩小此脚本。

Or do you think this is a bad idea? 还是您认为这是个坏主意? After all, it would result in static resources having to be passed through a php script before delivering them. 毕竟,这将导致在传递静态资源之前必须通过php脚本传递这些资源。

On-the-fly minification scripts are fairly advanced and use caching to ensure that scripts aren't minified on every single call. 动态的缩小脚本相当先进,并使用缓存来确保不会在每次调用时都缩小脚本。

You can use something like http://code.google.com/p/minify/ 您可以使用类似http://code.google.com/p/minify/的名称

If you are using a shared hosting maybe interesting this option: 如果您使用共享主机,则此选项可能很有趣:

https://www.minifier.org/ https://www.minifier.org/

and you could minify css or js on the fly. 您可以即时缩小CSS或JS。

According to my tests on the fly minification with that library: 根据我对该库进行动态缩小的测试:

  • execution time without on the fly minification: 0.0006 seconds 无需即时缩小的执行时间:0.0006秒
  • execution time with on the fly minification: 0.13 seconds and Save 20Kb 快速缩小的执行时间:0.13秒,节省20Kb

As you can see without minification (a simple echo) my sample script has been executed 200 times faster than using minification. 如您所见,没有缩小(简单的回声),我的示例脚本的执行速度比使用缩小快200倍。

You must assess the advantages and disadvantages according to your application but you could generate cached files and only use minification in the first call. 您必须根据您的应用程序评估优缺点,但是您可以生成缓存的文件,并且只能在第一个调用中使用缩小。

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

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