请注意,这个问题更多地是关于这个主题的理论方面,但除了去除空白之外,还有什么其他技术可以用于JavaScript压缩?
JavaScript压缩库使用哪些技术来最小化文件大小?
原标题:
最佳回答
大多数压缩机使用不同技术的组合:
- stripping whitespaces
- compress file by compression algorythm (gzip, deflate)
- most of the space is saved renaming interall variables and function to shorter names, eg:
此功能:
function func (variable) {
var temp = 2 * variable;
return temp;
}
将变成:
function func (a) {
var b = 2 * a;
return b;
}
- Dean Edwards packer uses some internal compression. The script is decompressed whhen loaded on the page.
- All the usual stuff to make a programmcode shorter:
- delete unused code
- function inlining
问题回答
从我的头顶。。。
- Tokenizes local variables and then renames them to a minimally-sized variable.
- Removes tons of whitespace.
- Removes unnecessary braces (for example, single line executions after if-statements can remove braces and add a single semi-colon).
- Removes unnecessary semi-colons (for example, right before an ending brace } ).
我最常用的迷你图是YUI压缩机和正如他们所说,这是开源的,所以你可以自己看看他们到底做了什么。(我不确定他们所说的“微优化”是什么意思,可能是一些罕见的情况,让你有一两个字符。)
代码重命名和重新排序,这样gzip压缩器将有更好的结果。
例如(不那么聪明)
原始代码:
function mul(mul1, mul2)
{
return mul1 * mul2;
}
function print(str)
{
// do something
}
function add(add1, add2)
{
return add1 + add2;
}
修改代码:
function mul(a,b)
{
return a * b;
}
function add(a, b)
{
return a + b;
}
function print(str)
{
// do something
}
我可以列举一些在Google Web Toolkit编译器中找到的内容:
- Inline of method calls
- Dead Code elimination
- Variable renaming/source obfuscation
- this means rewriting long variables to short ones and so on
几乎所有这些都需要Javascript解析(即它们的工作超出了纯粹的词法分析)。
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding