Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

javascript - Does it make sense to minify code used in NodeJS?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Minification can improve performance.

Node's V8 optimizing compiler inlines functions 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 9.0+ / V8 6.2+ (Turbofan) - minor performance improvements

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.

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

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

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

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

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

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

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

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}.

If we now call both variants n times and compare the performance of the raw and the minified function, we get the following result:

Raw vs minified performance

Obviously, the minified function performs more than twice as fast.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...