Does it make sense to minify code used in NodeJS?
Asked Answered
G

4

66

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.

Grosso answered 22/8, 2012 at 8:17 Comment(5)
That's more of an empirical thing to test than a question that can be answered definitively.Tabitha
I believe there's solid theory behind it, I just don't understand it... Too bad there's no such tool as jsperf for node...Statius
@João, you might be interested in #8616077.Carangid
Minifying code is usually done to conserve bandwidth. Since node.js runs on the server, I'd say it's meaningless to minify the code.Fatsoluble
@JoãoPintoJerónimo: As seen on jsPerf -> npmjs.org/package/benchmarkMiddlebrow
B
33

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

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

Here, the gap between calling readFile and 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.)

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

Bahr answered 22/8, 2012 at 8:26 Comment(4)
Closure-compiler's prototype devirtualization and function inlining (along with all the other optimizations) will cause the code to execute faster, but it would still probably be a small improvement compared to I/O operations.Neeley
The largest win I've seen is actually from collapse properties.Answerable
Let me get this answer straight. Im using express.js and gulp to minify my webapp assets, im realizing that app.js is NOT an asset, its the SERVER. I was going to uglify it too (app.js) and push into the build dir www/ which is node http hosted, but i think this post is stating that a node js server file shoud NOT need compressed, which means i prob should NOT be running it though gulp uglifying pushing to www/ at all then right? i can just lint it in root, and have only client-side stuff pushed to build dir? Its nice to have the server outside of the www/ hosted dir, i like that anyway.Cadmarr
Yes, that's right. It's not just "nice" to have the server outside of www, it's security best practice.Bahr
D
62

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.

Dispossess answered 1/7, 2016 at 0:28 Comment(0)
B
33

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

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

Here, the gap between calling readFile and 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.)

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

Bahr answered 22/8, 2012 at 8:26 Comment(4)
Closure-compiler's prototype devirtualization and function inlining (along with all the other optimizations) will cause the code to execute faster, but it would still probably be a small improvement compared to I/O operations.Neeley
The largest win I've seen is actually from collapse properties.Answerable
Let me get this answer straight. Im using express.js and gulp to minify my webapp assets, im realizing that app.js is NOT an asset, its the SERVER. I was going to uglify it too (app.js) and push into the build dir www/ which is node http hosted, but i think this post is stating that a node js server file shoud NOT need compressed, which means i prob should NOT be running it though gulp uglifying pushing to www/ at all then right? i can just lint it in root, and have only client-side stuff pushed to build dir? Its nice to have the server outside of the www/ hosted dir, i like that anyway.Cadmarr
Yes, that's right. It's not just "nice" to have the server outside of www, it's security best practice.Bahr
S
5

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.

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

Salmons answered 11/9, 2017 at 1:54 Comment(0)
R
0

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.

Rarefaction answered 18/6, 2019 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.