Blog about software development


Choose Java, Go or JavaScript for your API

26 Mar 2016 - by 'Maurits van der Schee'

In today's era of web based micro-services (HTTP APIs) building small simple high performance web servers is of vital importance. In this blog post background information is given on the expected performance of implementations in popular programming languages. This post will be followed up by attempts at high performance implementations in some of these languages (Java, Go, C# and JavaScript). Do not expect benchmarks or absolute numbers, but do expect source code that you can easily run on your machine, so that you can do the benchmarking yourself.

The benchmark game

From the benchmark game we may learn that there are two performance profiles for popular programming languages: that of compiled languages and that of script languages. The script languages ordered from fast to slow are: JavaScript, PHP, Ruby, Python, Perl. For the compiled languages the order is: C, C++, Java, C#. Go is a newer and less popular compiled language, but it performs quite comparable to Java. JavaScript needs to be mentioned for its performance, as it can be surprisingly fast.

Web Framework Benchmarks

When we look at web frameworks, then we see that many web frameworks also optimize for execution speed. I'm not sure I understand that as the language in which they are written more or less defines their maximum speed. So performance is not a feature. If a framework performs significantly worse than other frameworks in that language it should be considered a bug. Other than speed they seem mainly concerned with architecture and tests (and these are related). IMHO they should focus on "developer friendliness" and their "LoC to features" ratio (also somewhat related).

Library calls implemented in C

It is important for performance to know what part of your application is executing code in the language you are writing in. The language's built-in functionality is often highly optimized for speed. This is not true for your business logic and maybe some libraries you use. In JavaScript and PHP a lot of heavy functions (like regex and json parsing) are implemented in C and that is why those functions perform unexpectedly good in those languages. This is an important trick of script languages to improve the actual performance. How much speed you win depends on the ratio between built-in and added logic in your application.

Don't shoot yourself in the foot

As you can read in the title I do not recommend C or C++ for web API programming. I agree with Bjarne Stroustrup (author of C++) that programming in a language that is lacking a memory manager is hard. In C and C++ you have to request and release memory explicitly, so an error is easily made. If you want to go that way, it is interesting to check out kore.io, libmicrohttpd or to write Nginx modules (that can easily be prototyped in Lua). Also, it is recommended that you use tools like ValGrind to find memory leaks.

Links / References

PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.