javascript 缩小 NodeJS 中使用的代码有意义吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12068971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:13:58  来源:igfitidea点击:

Does it make sense to minify code used in NodeJS?

javascriptnode.jsminifygoogle-closure-compileruglifyjs

提问by Jo?o Pinto Jerónimo

I was wondering, since Clojure Compiler and UglifyJS not only optimize code for size but also for performance (although I think size is the main priority), would my node.js app run faster if it was minified ? I know it may depend from app, but I'm asking this in general.

我想知道,由于 Clojure Compiler 和 UglifyJS 不仅优化了代码的大小,还优化了性能(虽然我认为大小是主要优先事项),如果我的 node.js 应用程序被缩小,它会运行得更快吗?我知道这可能取决于应用程序,但我一般会问这个。

采纳答案by Nathan MacInnes

In node, the main processing cost is I/O operations, not the actual JavaScript itself. So for example:

在 node 中,主要的处理成本是 I/O 操作,而不是实际的 JavaScript 本身。例如:

fs.readFile(myFile, function (err, data) {
    processTheFile(data);
});

Here, the gap between calling readFileand the callback being fired will be several times longer than the length of time the callback takes. (If it's the other way round, you probably shouldn't be using node.)

在这里,调用readFile和回调被触发之间的差距将比回调所花费的时间长几倍。(如果反过来,您可能不应该使用节点。)

So optimising the processTheFilefunction for speed is pointless, because you're saving a small percentage of a very very small number.

因此优化processTheFile速度函数是没有意义的,因为您节省了非常小的数字的一小部分。

回答by le_m

Minification canimprove performance.

缩小可以提高性能。

Node's V8 optimizing compiler inlinesfunctions according to some heuristics. Minification influences these heuristics. This can cause inlining of previously not inlined functions. Since inlined functions generally perform faster, this can lead to performance improvements.

Node 的 V8 优化编译器根据一些启发式方法内函数。缩小会影响这些启发式方法。这可能导致内联以前未内联的函数。由于内联函数通常执行得更快,因此这可以提高性能。

Node 9.0+ / V8 6.2+ (Turbofan) - minor performance improvements

Node 9.0+ / V8 6.2+ (Turbofan) - 轻微的性能改进

If the function's unoptimized bytecode size is less than 500, it will be inlined. Minification generally reduces AST (Abstract Syntax Tree) node count. Since bytecode is directly generated from the AST, we can expect some reduction in bytecode size as well.

如果函数的未优化字节码大小小于 500,它将被内联。缩小通常会减少 AST(抽象语法树)节点数。由于字节码是直接从 AST 生成的,我们也可以预期字节码的大小会有所减少。

Source: [Turbofan] Use bytecode size for inlining heuristics.

来源:[Turbofan] 使用字节码大小进行内联启发式。

Node 8.3+ / V8 5.9+ (Turbofan) → minor performance improvements

Node 8.3+ / V8 5.9+ (Turbofan) → 轻微的性能改进

If the function's AST node count is less than 196, it will be inlined. Minification generally reduces AST node count.

如果函数的 AST 节点数小于 196,它将被内联。缩小通常会减少 AST 节点数。

Source: [turbofan] Don't take into account source size for inlining heuristics.

来源:[turbofan] 不要考虑内联启发式的源大小。

Node 8.2 and before / V8 5.8 (Crankshaft) and before → major performance improvements

Node 8.2 及之前 / V8 5.8 (Crankshaft) 及之前 → 主要性能改进

If the function's character count - including whitespace and comments - is less than 600, it will be inlined.

如果函数的字符数 - 包括空格和注释 -小于 600,它将被内联。

Let's say we have a function which is more than 600 characters long:

假设我们有一个超过 600 个字符的函数:

function f() {
  // A long comment... bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  return 1;
}

Minification reduces this to function f(){return 1}.

缩小将其减少到function f(){return 1}.

If we now call both variants n times and compare the performance of the rawand the minifiedfunction, we get the following result:

如果我们现在调用这两个变体 n 次并比较原始函数和缩小函数的性能,我们会得到以下结果:

Raw vs minified performance

原始与缩小的性能

Obviously, the minified function performs more than twiceas fast.

显然,缩小后的函数执行速度是原来的两倍多

See also: #NodeJS : A quick optimization advice

另请参阅:#NodeJS:快速优化建议

回答by Manuel K.

Based on the following two links it does make a lot of sense:

基于以下两个链接,它确实很有意义:

A quick optimization advice

快速优化建议

Test run on February 3rd 2017

2017年2月3日试运行

回答by Greg

No longer true.

不再是真的。

Yes, Node6 is now based on v8 5.1, which use TurboFan. As the v8 team stated (https://bugs.chromium.org/p/v8/issues/detail?id=3354) they dropped the character count trigger for inlining.

是的,Node6 现在基于 v8 5.1,它使用 TurboFan。正如 v8 团队所述(https://bugs.chromium.org/p/v8/issues/detail?id=3354)他们放弃了用于内联的字符计数触发器。

https://medium.com/@c2c/yes-node6-is-now-based-on-v8-5-1-7a645eb9992bhttps://bugs.chromium.org/p/v8/issues/detail?id=3354

https://medium.com/@c2c/yes-node6-is-now-based-on-v8-5-1-7a645eb9992b https://bugs.chromium.org/p/v8/issues/detail?id= 3354

回答by Kat Lim Ruiz

I created a nodejs cli which generates a new app with some pre-defined classes. I think in this case, it makes sense to minify the base code. Because you want to allow the developer to use it, but not modify it (or at least make it very hard to do). This way I would be pushing the developer to download the new version and not update the class within the app.

我创建了一个 nodejs cli,它生成了一个带有一些预定义类的新应用程序。我认为在这种情况下,缩小基本代码是有意义的。因为你想让开发者使用它,而不是修改它(或者至少让它很难做到)。通过这种方式,我将推动开发人员下载新版本,而不是更新应用程序中的类。