The great Node.js versus Ruby on Rails speed test

There is a server environment for web development I have heard mentioned a lot  called Ruby on Rails. Though I am just starting to learn JavaScript, I thought I would check it out because I have heard so much about it and one of my friends recommended it to me. I am sure for most programmers Ruby on Rails is already yesterday’s news, but it is new to me, so I thought I would give it a try. I would really like to learn just one language at a time when I am starting out, but Ruby on Rails sounded interesting, and maybe if it’s good I can try to write the web client in Ruby on Rails somehow too.

The first obstacle I ran into was how to get it installed. I am running on Windows so my options are limited. The very first option on the Ruby on Rails web site is to compile from source code! Are they actually trying to be hostile to beginners?

They had the option to download binaries for Windows, which took me to this page which is a big jumbled up list of Ruby versions and files (do I want 1.8.7 or 1.9.3?), trying to figure out how to get Ruby gems to work, and not having a whole lot of luck, I finally found Rails Installer which did everything for me and was pretty nice overall. It was still much easier to get Node.js installed, and I’m really confused to why the rubyonrails.org web site links to the site with a big list of files instead of Rails Installer.

The main thing I was worried about was speed. I had heard Ruby is something like the slowest language on the planet, so if my game server is going to need one server for 50 people with Node.js, how many is it going to need for Ruby on Rails?

I had the Node.js hello world example loaded, and opened up the Chrome Network tab so I could measure the speed of Node.js as compared to Rails. I loaded the page with the network tab open and checked the time: 2ms. Pretty fast! (I think!)

The next thing to do was to get a hello world working in Ruby on Rails, since unlike the Node.js web site they do not post a Ruby on Rails hello world example code on rubyonrails.org for some reason. I tried to google around for how to get Hello World going, and found this link to the Ruby on Rails wiki that was down (and I’m beginning to think Ruby on Rails is a bad choice) and when I finally got it to work, the whole Wiki was empty and full of spam. Ruby on Rails documentation seems to be pretty bad, which kind of surprises me because it’s so old, you think they would have good documentation by now, and it makes your web framework look pretty bad when your own wiki won’t load and is full of spam.

Eventually I found this blog post that had nice step-by-step instructions, and I finally had my hello world example up and running and even loaded in my browser.

Well, it was time for the moment of truth. How fast is Ruby on Rails compared to Node.js? It took 309ms! In case you can’t do math that’s like 150x slower than Node.js. I figured this was just a fluke and maybe if I refresh it will be faster, and it was, but it was still 20ms, which is 10x slower than Node.js. So Ruby on Rails not only starts up really slow, even after it gets running it’s still very, very slow, and this is just doing Hello World. If my Node.js server can handle 50 users, does that mean a Ruby on Rails server will only be able to handle 5?

The more I googled around the more I saw a pattern between the Ruby on Rails and the Node.js programmers: Ruby on Rails programmers don’t care about speed, and Node.js programmers do! When I would google for speed and Ruby on Rails I saw lots of people dismissing speed and talking about how it’s not important. Node.js is not like that and really seems to value performance. Since I’m writing a game and I want it to be fast Ruby on Rails was probably a bad choice.

I did some further reading on Node.js versus Ruby on Rails and found this blog post which confirmed my suspicions. This post was written by a Ruby on Rails expert who decided to switch to Node.js, so I knew he wasn’t just saying that stuff because he likes Node.js, he has hands on Ruby on Rails experience.

Ruby on Rails was created in the days before HTML5 so it has a lot of legacy around the old way of making web applications, which isn’t with a JavaScript frontend, but with HTML pages assembled by the server. Node.js is a much newer technology and so it doesn’t have this legacy, and is instead built on modern standards. Also Node.js has event-driven non-blocking I/O which is probably the main reason it’s faster than Ruby on Rails.

Then I found the real clincher: Ruby on Rails doesn’t support Socket.io! They want me to use AJAX instead, which is old and slow, and there’s no way I’m going to use it to write a game.

And so ends my brief investigation of Ruby on Rails versus Node.js. I am fully confident going forward that Node.js is the right choice for the backend of my game, and best of all I don’t have to learn two languages when I could just use one. I am very excited to try out the Chilly Framework for building HTML5 games as it sounds like exactly what I need.

Long story short, if you are trying to build an HTML5/JS game, don’t even bother looking at Ruby on Rails, because it’s too old and two slow and makes you learnt at least two languages on top of everything else.

Down the JavaScript rabbit hole

I’ve always been really smart about computers, but programming is one thing that always eluded me. Every time I sat down to start, and try to learn, it was just too much, and I spent hours reading without really learning anything. Well, no more excuses, I’ve decided that 2012 is the year I am going to learn how to program.

If you know me, you know I love games, and games are what I want to make as a programmer. There’s lots of languages for writing games, so what’s the best one to pick? I used to think I knew the answer to that question, C++, but now I’ve changed my mind.

Since I want to make a startup around games I was looking for a way to write games that would make me money. This means a low barrier of entry, both for myself learning how to program, and also a low barrier of entry for publishing and selling games.

To make the most money the first thing I looked at was the iOS platform. I own an Android phone, but I know there’s more money to be made on iOS. I quickly learned the main language of iOS isn’t C++, it’s a completely different language called Objective C which only works on Apple devices (leave it to Apple to invent their own programming language). The more I read, the more I learned that I would probably have to learn both C++ and Objective C to write games for iOS, since you need to know Objective C for Apple, but most gaming libraries are in C++. Screw that, I’m going to have a hard enough time learning one language, let alone two!

Then came the real kicker: if you want to develop iOS apps, you must own a Mac, because the software to make iOS apps is only available for Mac! I’m a Windows guy and I have no plans on switching to Mac any time soon (I don’t like wasting money on inferior computers), so that was it. Plus the more I read I saw both Objective C and C++ have manual memory management instead of a garbage collector, and that sounds like a big pain in my ass! I want to be making awesome games, not manually managing memory!

So iOS was out. I got to talking with some of my friends who are programmers and they told me that you can develop games directly for the web with HTML5 and JavaScript. I was generally familiar with JavaScript, but I hadn’t heard anyone was writing games with it. Most of the games I had seen on the web had been written in Flash, and I hate Flash.

JavaScript as a language for games sounded interesting. I didn’t know much about JavaScript besides it runs in web browsers, and had not seen it had advanced new features for game authors, but I began checking it out and found something that completely blew my mind:

BrowserQuest is an MMORPG developed by Mozilla which you don’t have to download or install because it runs right from your web browser. It is written completely in HTML5 and JavaScript, and best of all it is multiplayer! People from all around the world can play at the same time, and it all shows up in realtime just like any installable game written in C++.

Then I realized something I hadn’t thought about: if I want to write a game like this, I will need to write a server too! Argh! What language am I going to use for that? I was already back to having to learn C++ and Objective C again. Can it run on Windows or do I need to figure out how to deploy on Linux? How do I make it talk to my game client running in the web browser?

As I dug into BrowserQuest more I made an amazing discovery: JavaScript can run on the server too. You can use one language to write the client and the server, and that language is JavaScript. The server-side JavaScript environment is called Node.js, and it runs perfectly on Windows so I don’t need to learn about Linux servers just to run my game server.

BrowserQuest is written in Node.js so I knew it would scale, and also reading about Node I found out it has a lot of advanced features like V8 JavaScript engine from Chrome, and event-driven non-blocking I/O which scales better than threads. Looking at the BrowserQuest status page it looks like it takes 4 Node.js servers to support about 200 people, which seemed like kind of a lot but doesn’t seem completely unreasonable. I didn’t plan on having that many people in my game right away, but I knew that one server would support at least 50 people.

I still wasn’t sure how to make the game talk to the server. I knew a little bit about AJAX, but that was so old that I assumed there’s something better out now, and there is. I discovered Socket.io, which makes it really easy for Node.js programs running on the server to talk to the client. In fact this is all you need to make it work:

SERVER

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

CLIENT

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

With my newfound knowledge that I could learn one programming language to write both the client and server of my game, I have decided that JavaScript is the programming language I am going to learn.

I am still looking for good resources on how to learn JavaScript, so if you know any good books you can recommend, please leave them in the comments as I’m a total n00b 😉