Comparison of Node.js with other alternatives for a simple project
adnan360 45914f9be5 Remove HTML::Entities module dependency | %!s(int64=3) %!d(string=hai) anos | |
---|---|---|
gulp | %!s(int64=3) %!d(string=hai) anos | |
manual | %!s(int64=3) %!d(string=hai) anos | |
nodejs | %!s(int64=3) %!d(string=hai) anos | |
perl | %!s(int64=3) %!d(string=hai) anos | |
perl_text_template | %!s(int64=3) %!d(string=hai) anos | |
LICENSE | %!s(int64=3) %!d(string=hai) anos | |
README.md | %!s(int64=3) %!d(string=hai) anos |
This is basically a look into Node.js vs Gulp vs Perl in terms of speed, usability and possibly some other aspects.
To get a total run time of generating the page 100 times with an implementation, we basically do:
cd <implementation>
n=0; time while (( n++ < 100 )); do rm public/index.html ; ... generate command ... ; done
Tested in bash shell on Ymir GNU+Linux (similar to Void Linux) x86_64 under average load. Feel free to test yourself and post an issue with the results.
manual
directory has the unautomated version of the project. For this, a project named "polytable" has been used (name may change in future). It's basically a cross-language syntax explorer displayed in a table-like UI. All the output generation is basically done with JSON data (src/info.json
) and code examples are individual files. There are million ways to do this, but this seemed like a maintainable system. Any language that can deal with this setup, can be included here. And it should be as close to 0-install as possible.
These languages/implementations took these many seconds in average to generate output of a basic project 100 times:
Gulp: 1.371
Node.js: 0.111
Perl: 0.081
Perl (Text::Template): 0.092
I was thinking of moving away from Node.js/Gulp because they seem too much to handle sometimes. For example, when I get to a new system, I have to install node, gulp-cli, optionally pnpm to get higher performance and lower disk space usage, then install all the node_modules with pnpm i
. All these just to generate a basic static webpage seems like a lot. Gulp is probably great for large projects and complex setups, but it seems a bit overkill for small projects. And as I found out Gulp is slow too (although it could be my fault, the way I coded things, used modules etc.) So I was looking for an alternative and if possible 0-install solutions out there.
Pure Node.js implementation (under nodejs
) does not use any modules and is faster than Gulp. But there is a catch. Too keep things simple it does not use a templating engine. It might be the reason for this improvement. Node.js is not 0-install, but I guess I can live with that. Besides it is more common, so I can reuse the installed Node.js in some other project and I'm not installing any modules. So, not bad.
Recently found out Perl is installed in most Linux/Unix systems. Too add to that Perl is faster than Python on almost every basic things I usually do. After I found out I thought why do I use Python then? On some minimal systems python3 isn't installed by default. If Perl is already installed in them, well, one less install to care about.
In the results Perl is the fastest. Less than 0.1 seconds - it's basically blink of an eye. Although it does not use a templating module as well. It has separate template HTML files which it parses and "glues" with the final output, similar to Node.js implementation. Although I'm interested to try out lightweight templating engine(s) in the future. (Update: Text::Template already tried.)
Another thing where Perl has a good position is that it has modules to handle JS/CSS minification. If I need that for some reason I can probably use it. I'm sure there are loads of other things out there which can compete with Node.js/Gulp modules.
I'm not an expert at optimizing code in all these languages, but the time could probably be improved. If you think you can make it work faster, feel free to post an issue or perhaps a PR.
cd nodejs
n=0; time while (( n++ < 100 )); do rm public/index.html ; node generate.js ; done
Returned 11.099
so 11.099 / 100 = 0.111
cd gulp
pnpm i
n=0; time while (( n++ < 100 )); do rm public/index.html ; gulp ; done
Returned 137.103
so 137.103 / 100 = 1.371
cd perl
n=0; time while (( n++ < 100 )); do rm public/index.html ; ./generate.pl ; done
Returned 8.081
so 8.081 / 100 = 0.081
Perl (Text::Template)
This is using the Text::Template module.
cd perl_text_template
n=0; time while (( n++ < 100 )); do rm public/index.html ; ./generate.pl ; done
Returned 9.193
so 9.193 / 100 = 0.092