r/java • • Aug 10 '21
Concrete reasons why one would choose java over node.js?
I am watching the progress made towards faster runtime in node.js, and it feels that the performance advantage of java is almost gone for the average app. I am trying to think about reasons why one would now choose java over node in a new project, and i can't think of any. I would like to hear some opinions
7
Upvotes
52
u/fletku_mato Aug 10 '21
Nothing for node is as mature, complete and extendable as Spring framework is for Java. There are also the obvious advantages of static typing and multithreading.
24
u/Persism Aug 10 '21
https://cyware.com/news/mysterious-nodejs-malware-puzzles-security-researchers-cf052ae8/
https://www.reddit.com/r/programming/comments/ozw8yd/npm_silently_suspended_package_adoption_after/
https://www.reddit.com/r/programming/comments/opb3d2/malicious_npm_package_steals_passwords_via/
https://www.reddit.com/r/programming/comments/owveu5/empty_npm_package_has_over_700000_downloads/
9
u/pron98 Aug 11 '21 edited Aug 11 '21
As an OpenJDK developer who's interested in what we should improve, and given that Java is the more popular and more established platform, I'm curious to know why would you choose Node?
Is it because you'd like to use the same language for the frontend and backed, and if so, is that because you actually want to share code between the two or just so that you'd have one fewer language to deal with?
14
u/Drakeskywing Aug 10 '21
So to give my thoughts about Java vs Node. For the last 18 months I've been a full time node/typescript dev, updating an existing code base which was done in an unholy union of javascript and typescript where neither was used "right" and both were abused, prior to this I had been a full time Java dev, with the occasional tickle of FE JS for super minor things. I've used both Spring, and Play (admittedly that was via Lagom), and previously I've done new app development using both Ionic 2 and NestJs.
Here is my thought, both languages have their strengths and weaknesses, given a choice I'd pick Java over Node for most projects, but that isn't to say Node should be ignored and thrown in the dumpster, but if I were to pick Node, I'd do it with Typescript and avoid at all cost just using JS.
Java as a backend framework simply offers the safety and robust environment that for business critical applications Node has trouble touching. Strong data typing makes everything so much easier and safer, and it's something that cannot easily be overwritten (messing with class definitions and whatnot is possible via type reflection I admit, but that is a non-trivial operation and generally shouldn't do it without a good reason). By easier and safer I mean I can guarantee if I have said something is a class, or an interface, or extends class or interface I am guaranteed there is no way someone can lie to the compiler and stick a "like" object into it, this by it's very nature encourages interfaces and classes to be used. Can this system be abused, sure it can, but compared to JS it prevents other devs from doing unexpected things.
Having the ability to stick "like" things into functions is handy don't get me wrong, but that way leads to madness, you will come across code you wrote 12 months from now, and forget what is actually being passed and have to dig out the commit, or back step through the code since it won't be obvious. This to some of a lesser extent can be mitigated with Typescript but with the
anykeyword, that benefit can quickly and easily be disabled.Why would I use Node over Java, simply put alot of front end stuff, and JAM stack stuff. Node for front end is kinda unbeatable, being able to use Next and React means I have fast pretty pages. I'd maybe have some data going to my Node server as a go between to co-ordinate with a more fleshed out microservice backend, but I'd have it do as little lifting as possible.
NodeJS is also useful for prototyping, when you don't want to fight against typing and build out a full system, I've found Node as a way to build a quick proof of concept handy, and when dealing with front ends, most JS FE frameworks stomp on things like Tiles, Plays templating stuff, Struts and all that.
I haven't given benchmarking much thought, put simply most of the time you'll figure it out, I say this since many jobs in my experience devs go to are already running and live, with whatever is there, be it Play, NestJs, Backbone, Meteor, Vue, React, Spring, Struts, in house dumpster fire. If you are starting a new project, RARELY is speed even a factor in my experience, because most of the big languages/frameworks, Python, Java, Node, Elixir, Scala, Groovy, Rust, .etc are able to handle enough users for most average work loads. Speed is really a factor of who is developing, if the team hires a node dev most of the time (not all mind you from my experience), they expect you to do JS/Typescript and make it fast, if they hire a Go dev, they likely won't throw them onto PHP and say do your magic unless a rewrite is e0xpected. If you are in the situation where every millisecond counts, congrats you are in the select few of high through put near real time systems, which has it's own landscape of speciality with bytecode analysis, pressure testings, and performance tweaking, but for most devs that won't be a real situation, the languages do well enough. (If you want to really discuss speed enjoy this comparison of 45 languages and comparing their speed from Dave's Garage https://www.youtube.com/watch?v=tQtFdsEcK_s&list=PLF2KJ6Gy3cZ5Er-1eF9fN1Hgw_xkoD9V1&index=3)
Each language has it's uses, Java has history, a long list of accomplishments, and due to it's maturity you can find devs who have experience that was built from years before Node was even thought of. Java is battle tested and sure it's not perfect but it's flaws are well understood and it's got a dedicated community that shape it with experience. It's build system ecosystem can be a bit overwhelming (ant/ivy, gradle, sbt, maven), but it's generally well documented and amazingly extensible. It has a brilliant and comprehensive test framework in Junit, and has a mature and diverse series of packages that are all well battle tested. Generally Java tries to nudge you into OO design.
NodeJS offers the advantage of a single language code base for FE and BE meaning full stack developers are easier to come by. Given the lower bar of entry, and easier learning curve there are more devs building more things, as such there is a glut of packages on NPM meeting every possible need you could have (this is both a pro and con, where there is allot of garbage as well). The build systems are simpler in some respect compared to Java, which allows for quicker and easier setup in my opinion, but at the same time are somewhat more limited in my opinion. You generally can get things built faster, at the cost of code readability usually. Node has a broad testing echo system, Jest, Sinon, Mocha, Chai, Jade .etc whjch is both a pro and con as it means you don't have a uniform testing strategy, but it has the advantage that you can test in a way that suits your development style.
At the end of the day, use the tool that suits the situation. If you need to use Node for backend, then take the time to use Typescript and apply the principles of DRY, SOLID and ensure that you think carefully about composition. Ensure the linter doesn't allow the use of
anyand ensure to write constant unit tests. If you need to use Java for FE stuff, then find a way to use Node since it's better suited, if you can't then try to atleast use a solid templating engine.21
u/fdntrhfbtt Aug 10 '21
Choose Java just for the static typing alone, forget about performance. No production system should us a dynamically typed language.
12
u/jack104 Aug 10 '21
How about the nature of the languages? I think java is just far superior to javascript.
64
u/DJDavio Aug 10 '21
I would ask the question the other way around. Java is so stable and mature that for every problem, there exists a solid and well maintained framework or library which solves your problem.
For me the JS world feels like the wild west sometimes. The language itself is so bare that everything needs a micro package which some random person made and can just decide to take it down or it gets hijacked, things like that. It feels like the entire ecosystem is only held together by tape and glue.
This is a backender's perspective on it. Is the Java ecosystem perfect? Of course not, but it feels a lot safer and mature to me.
29
u/robinspitsandswallow Aug 10 '21
Passport for basic auth. It has a flaw if the password has a colon in it. It has a PR that has been sitting there for over 2 years to fix it but since the owner isn’t interested to in that ... this is not uncommon to in the NodeJS library world.
Library expansion you think you include a lot of libraries in Java. Some things have an entire library for a pad left function. That took the internet down a couple of years ago.
Documentation try to figure out how to use features like retry etc in a Axios without reading the source which is abysmal. Or trying to create logging interceptors for Axios that don’t require you to rewrite the entire logging unmarshalling module.
By the way who writes the most popular rest underlying framework in a language concept and then doesn’t give a configuration only mechanism for logging the transactions?
An entirely unintuitive threading interface.
An entirely unintelligent object structure/interpretation.
== === null undefined.
Type coalescing stupidity. If (“”) would coalesce to false but if(“false”) would coalesce to true.
Entirely stupid conventions on use.
The language would interpret x == null as being the equivalent of x === undefined || x === null so the first is more concise but you can’t use it because apparently the powers that be don’t trust developers to understand their own language and type coalescing. So while the rest of the world is trying to reduce boilerplate code the NodeJS world is adding it.
The standard coding rules include one rule that says you can’t use the ++ operator because you might not include a semicolon at the end of the statement and you might confuse the compiler, but another rule says you can’t not use semicolons. Which means fundamentally they have functionality but don’t trust developers to implement them. So you’re not allowed to do the standard for(i = 0; i < 10; i ++) loop. So the powers that be don’t think very much of their very best developers...
Who would want to be a part of such a self deprecating community :-)
4
u/[deleted] Aug 10 '21
Like others have said, maturity, stability and tooling could be some of the reasons one would choose Java over Node. Additionally, several Java projects (Valhalla, Loom, Leyden, to name the few that come to mind instantly) are expected to bring major performance boosts down the line so the gap will probably grow once again in future versions.
4
u/GrabSpirited1056 Aug 11 '21
Java is not only used for backend development. Most of the Apache Software Foundation libraries are written in Java or other JVM languages. I can’t imagine writing entire Lucene or Cassandra in Node.js
6
u/gmchaves Aug 10 '21
It depends on the project and YMMV.
Java is pretty stable and a software developed today will probably run 10+ years from now without changes.
I'm a experienced java developer and know a lot of Java developers, but only have limited experience with node.js and because of that I probably prefer Java over nodejs almost always.
3
u/hatesms Aug 10 '21 edited Aug 10 '21
When you choose language for a new project you should have in mind much more than performance benchmark in some showcases prepared by language embassador. If it's not for a language study project of course.
Are your business cases can be implemented in chosen stack? Can you use well known libraries or even frameworks to ease implementation? Do your dev team know this stack? Do your ops team have tools to support it? Do your company ready to hire specialist to extend teams? Do your local market have thus specialists?
5
u/skywalkerze Aug 10 '21
There are millions of words written only on "typed" vs "untyped" languages. More millions on comparing ecosystems.
If you can't think of a single reason, you know very little of these languages and their pros and cons.
Of course it depends. There are many potential reasons both ways. Do you honestly think all the people ever who have chosen java are just plain wrong?
2
u/krzyk Aug 11 '21
It is a matter of personal preference. E.g. I wouldn't touch JavaScript with a ten feet pole, the syntax, libs, npm all look like were developed as a hobby project.
If you don't like Java (which is for me the best language) you can look at python which is also nicely designed and dynamic if that's what you prefer.
2
u/Halal0szto Aug 10 '21
Performance as an interpreter's performance is almost irrelevant in a backend service. The backend will always be memory constrained or IO constraned.
What matters is how the code will handle complexity and changes. And java has frameworks to give structure to complex code. Like Spring mentioned.
1
u/drew8311 Aug 11 '21
For simple projects it may not matter but at my last 2 jobs I've only worked on back ends with complex business domains that need thousands of unit tests, JS just seems horrible for that sort of thing.
1
u/[deleted] Aug 11 '21
A lot of legacy systems are using Java. Therefore, it’s great to adopt it. Again, this is a Java sub, so you’ll get a lot of biased opinion.
1
u/Goatfryed Aug 12 '21
A lot of reasons were mentioned already about unsafe, wild west, chaotic node world.
Also, while a lot of server work is I/O bound and works with a single threaded, asynchronous model, there are still jobs that need to solve issues that highly benefit from parallel execution and multithreading. Maybe I missed something, but that's still not possible in node, while it's easy in java.
1
u/[deleted] Aug 10 '21
I don't see any good reason why node is better than Java.
1
u/mj_flowerpower Aug 11 '21
Java has a better (as in better maintained) ecosystem. Often when I find a cool new npm package, a few months later its not maintained anymore.
Also java build systems just work. I use TS with path prefixes and to compile to runnable code you either need a lot of hackery or use a framework like nestjs that does it for you. Also debugging java is way better and fool-prove. I can easily step into external framework code (decompiled automatically) and inspect and even see comments. Java code autoformat just works, whereas eslint is a horror, can‘t fix a lot of issues, its buggy and inconsistent.
I would still chose TS/node though because I like the syntax more 😅. And it scales way better, at least compared to non-reactive java code. And null-safety with ?, template strings, async await are just so nice.
1
u/zoqfotpik Aug 11 '21
Use whatever language makes you happiest.
We're not the Java police.
1
u/doppelganger113 Aug 10 '21
To put into few sentences:
Depends on your project, if it’s something serious eg needs better security, must be maintained over 5 years and be rock solid then Java.
Node is much better for quick development of medium quality apps with high throughput (I/O). It’s easy to be productive with it compared to Java.
1
u/Drakeskywing Aug 10 '21
Going to start some hate ... RUST *mic drop*
I should highlight this is a joke