Node.js vs Golang: Battle of the Next-Gen Languages

Node.js vs Golang

Recently, there have been criticisms about the value of using Node.js in a high-performance, networked application environment, and some developers have moved to the Go language.

There are also several high-profile corporate examples switching to using Go, beyond the creator of the language, Google. Dropbox has moved to Go, claiming significant performance increases, and the developers of the Docker virtualization system are also using Go.

It’s important to note, however, that Node.js is still used by a much wider audience, has more modules, is easier to use, and isn’t going anywhere anytime soon. Also, if you’re making a web application, Node.js will probably be your language of choice, as Go is currently used more as a scripting language when focus on concurrency and speed is the priority.

In this article, we will take a look at the differences between Node.js and Go from the developer’s point of view to uncover the strengths and weaknesses of each environment.

Node.js and the Javascript Language

Node.js Emblem

Node.js emblem

Node.js (and its recent fork, IO.js) is an increasingly-popular platform built on a fast, JavaScript-based runtime: V8.

V8 is a JS virtual machine created by Google that is designed to build scalable, networked applications. It compiles JavaScript code to native machine code, using some complex optimizations. V8 also does the memory allocation and garbage collection of JS objects.

Though these dynamic optimizations for a dynamically-typed language offer a lot of flexibility for developers, they do come with some performance costs at times.

As Node.js is written in JavaScript, it inherits most of the properties of that language. JavaScript is so popular these days that the ECMAScript standard development is done mainly in an evolutionary fashion, rather than a revolutionary fashion.

This has some important side effects for JavaScript and Node development:

  • Some of the JavaScript language flaws are difficult to fix without breaking backward-compatibility, though the worst issues are being fixed.
  • Useful, but radical, JS language features may not be implemented or only be “patched on” as an add-on syntax. This can lead to a messier language design compared to languages that integrate features cleanly into their design. A good example of this is concurrent computing with JS. JS best practices for concurrent computing went from a concurrency API (using event-based callbacks), to the devolopment of promises, to the coming support for async generators. These days callback hell is still a serious issue with many Node.js applications; however, this has led to less readable code, slower development, and perhaps even more bugs.

All this means that the JavaScript language evolves rather slowly (and some would say poorly too), even when good concepts from other languages are known to work better.

Node.js still has a huge community and tons of great applications being written for it, so we don’t want to scare you away with some of its pitfalls in small cases; it’s still a great platform to work with, according to the vast majority of developers.

The Go Language or Golang

The golang gopher

The Golang Gopher

Go, which is also referred to as “Golang,” is a compiled systems-oriented programming language started by Google in 2007. Go can be considered the result of a rather conservative language evolution from languages such as C and C++.

Go improves upon some of the misgivings of those languages:

  • poor dependency management
  • cumbersome type systems
  • difficult memory management
  • lack of parallel computation support
  • lack of multi-core support

Go also reduces the amount of code typing needed by being more expressive than C or C++.

Go makes it much easier to write robust, networked applications, without sacrificing much in the way of performance, compared to C or C++. The high performance is in large part due to the static compilation of the statically-typed Go code. A lot of optimizations are possible when a compiler can do all the code inspection work beforehand, as opposed to the dynamic JS compiler work done during runtime.

Golang vs Node.js Comparison of Features

We can begin to see why, for some types of application development, it might be useful to use Go instead of Node.js; however, Node.js is still a very useful language in many use cases.

Let’s compare the strengths and weaknesses of each programming language, so it may be easier to choose which environment is the right tool for your next job.

Go Node.js
Maturity Mature and robust for its age Mature, but the API is still somewhat changing. The recent IO.js Node fork changes might also become a cause for API problems for developers writing and using Node modules. How big this problem will be is not clear right now.
Performance Similar performance characteristics as with C or C++, which is to say very good Due to JS’s dynamically-typed nature, Node can not reach the raw performance of CPU or memory bound tasks that Go can achieve in many tests. However, in the common use cases where network communication or database interaction are involved, the performance of Node and Go are often equal.
Concurrency Go uses coroutines called goroutines. A goroutine is a lightweight thread managed by the Go runtime. Communication between goroutines is done very elegantly using channels. For more, see this video. Node suffers from JavaScript’s less than elegant concurrency support using the event-callback mechanism. However, for a lot of applications, working with JS promises and the coming async generator support (also called “semi-coroutines”) will suffice. Something like the Koa framework is already supporting the async generator approach in Node.
Scalability Go was really designed for scalability and concurrency, without too much hassle. Several people and companies using Node.js have made claims that Node has some problems in environments which need to scale massively. Perhaps Node can overcome these massive scaling issues in time.
Ease of Development People coming from a JS/Node background will need to learn some new programming concepts, such as: coroutines, channels, strict typing (with compilation), interfaces, structs, pointers, and some other differences. For a JS developer, it is really easy to get into Node programming.
Frontend & Backend Though you can run Go code in the browser using gopherjs, JS still is the way most developers like to program the frontend. Go is aimed more at the backend, in particular for developing high-performing concurrent services on the server-side. Not having to do a mental context switch while developing a JS-based client-server system is really nice.
Packages & Tooling The number of standard Go packages is growing steadily, currently at over 100, and the Go community packages can be searched easily. Though there aren’t as many different developer-friendly application frameworks to choose from as compared to Node yet, you can take a look at the “go get”-able packages from the Go community, which currently sits at over 58,000 available for use and growing. The number of Node packages sits at more than 100,000 currently. This means that a lot of groundwork has been done, and that can make certain software projects a lot easier and/or cheaper to implement. Node’s tooling is also superb. The npm package manager learned from all the package managers that came before and did most things right.
Developer Mindshare A recent study showed the rise of Go to a place in the top 20. The pace at which this happened exceeded expectations. Go definitely has a lot of momentum building up, especially with the recent support of the Android mobile OS. Node is much more popular at this time by many orders of magnitude. Getting good hosting support, commercial support, and Node.js freelancers for your project will be much easier for NodeJS at this point in time.
Error Handling Error handling in Golang requires you to implement explicit error checking, which can make error troubleshooting difficult. However, some argue that you get a cleaner application overall, once you understand Golang error handling. Error handling in Node.js can be inconsistent at times, but it does offer the more common throw/catch mechanism for errors that developers are accustomed to using with other languages.

Summary of Node.js vs Golang

The choice between Node.js or Go very much depends on which type of development suites you best and how massive the network service needs to scale.

Go may not yet have all community packages or mindshare that Node has, but its syntactically cleaner concurrency model, better raw CPU- and memory-bound performance, and its ability to scale up better with concurrent loads could make for a better foundation for certain network application types.

If you need certain Node.js packages that are not yet available for Go and that would be difficult or expensive to re-implement in Go, then Node may be the wiser choice.

If you feel like installing Go on your Ubuntu/Linux system, have a look at our guide to installing Go. Then visit GoByExample and the online Go book for some more great tutorials.

If you wanted to give Node.js a shot, be sure to check out our guide to installing Node.js, as well as our list of free Node.js hosting services. Then head on over to NodeSchool.io for some great tutorials on programming with Node.js.

Questions or Comments? Ask Jacob!

Ask a question and Jacob will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.