uglifyjs 通过将表达式移动到 `if` 条件子句有什么好处?
What does uglifyjs gain by moving expressions to the `if` condition clause?
代码如下
console.log("foo");
if (window.x !== window.y) {
const x = "x";
console.log(x);
}
使用 uglifyjs 缩小到
if(console.log("foo"),window.x!==window.y){const x="x";console.log(x)}
可以看出它并不比更直接的长
console.log("foo");if(window.x!==window.y){const x="x";console.log(x)}
那么他们搬家有什么好处呢?是一些棘手的特定于引擎的优化还是有我看不到的原因?
正如@GOTO 所指出的,第二个变体的长度可能相同,gzipped 后更长:
$ echo 'if(console.log("foo"),window.x!==window.y){const x="x";console.log(x)}' | gzip | wc --bytes
74
$ echo 'console.log("foo");if(window.x!==window.y){const x="x";console.log(x)}' | gzip | wc --bytes
76
其他人将我指向他们自述文件中的一个地方,他们在其中进行了澄清
consecutive statements in blocks are merged into a sequence; in many cases, this leaves blocks with a single statement, so then we can remove the block brackets.
所以这里不是这种情况,但是当你有类似
的时候它会节省2个字符
if (<expr>) {
console.log("foo");
if (window.x !== window.y) {
const x = "x";
console.log(x);
}
}
代码如下
console.log("foo");
if (window.x !== window.y) {
const x = "x";
console.log(x);
}
使用 uglifyjs 缩小到
if(console.log("foo"),window.x!==window.y){const x="x";console.log(x)}
可以看出它并不比更直接的长
console.log("foo");if(window.x!==window.y){const x="x";console.log(x)}
那么他们搬家有什么好处呢?是一些棘手的特定于引擎的优化还是有我看不到的原因?
正如@GOTO 所指出的,第二个变体的长度可能相同,gzipped 后更长:
$ echo 'if(console.log("foo"),window.x!==window.y){const x="x";console.log(x)}' | gzip | wc --bytes
74
$ echo 'console.log("foo");if(window.x!==window.y){const x="x";console.log(x)}' | gzip | wc --bytes
76
其他人将我指向他们自述文件中的一个地方,他们在其中进行了澄清
consecutive statements in blocks are merged into a sequence; in many cases, this leaves blocks with a single statement, so then we can remove the block brackets.
所以这里不是这种情况,但是当你有类似
的时候它会节省2个字符if (<expr>) {
console.log("foo");
if (window.x !== window.y) {
const x = "x";
console.log(x);
}
}