Exploring the modern JavaScript backend stack

JavaScript Zen

I just released coffee-watcher - a little script that can watch a directory and recompile CoffeeScript files if they change. What's interesting about this isn't the code itself, but the parts that are used build this script. In this post I'll dig deeper into these parts and explain how they fit together to form the JavaScript backend stack.

The JavaScript backend stack

The stack I used for coffee-watcher consist of following tools and libraries:

  • Google V8 JavaScript Engine:
    a JavaScript VM that makes JavaScript one of the fastest languages available today
  • node.js:
    a non-blocking I/O framework for V8. It makes it easy to implement realtime web-applications that can scale. With node you can also use JavaScript as a system scripting language
  • CoffeeScript:
    a minimalistic language that compiles into JavaScript. It makes JavaScript much more expressive and fixes a lot of annoyances
  • npm:
    node's package manager that makes it easy to install and share packages. It's similar to Perl's CPAN, Ruby Gems or Python's PyPi
  • docco.coffee:
    a little script that makes literate programming possible and beautiful

Now let's dig deeper into each of these.

Google V8: State of the art JavaScript VM

V8 is a stand-alone JavaScript virtual machine used in Google Chrome. It features following things:

V8 is lead by Lars Bak, the former technology lead of Java's HotSpot VM (HotSpot has made Java one of the fastest languages).

V8 is under rapid development and last version improved the performance by at least 50%. A New Crankshaft for V8 blog post features a nice graph that shows just how much V8's performance has improved over its very short life:

JavaScript V8 performance evolution

The bottom line is that V8 is under rapid development and is quickly becoming the fastest VM ever created. It's a great foundation for the JavaScript backend, especially when coupled with node.js.

node.js: Non-blocking IO and more

node.js implements following things on top of V8:

Personally, I have used node.js to implement a Comet solution for Plurk that scaled to over 100.000 concurrent users. I implemented it in a few days and it's not because I am good, but because node.js is very easy to use.

To show you just how simple node.js is here is a Hello World web-server:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(8080, "");

console.log('Server running at')

The non-blocking nature of node.js is a big deal. Here is a graph that shows how nginx (a non-blocking server) scales vs. Apache (a blocking server):

Apache vs. nginx
The graph is from a hosting company and represents real connections, you can read more in their blog post:

CoffeeScript: The beautiful way to write JavaScript

CoffeeScript Logo

CoffeeScript is a minimal language that compiles to JavaScript, it mimics JavaScript and aims to expose the best parts of JavaScript.

For me CoffeeScript feels like a much improved version of JavaScript where the bad parts are removed or replaced. CoffeeScript also moves JavaScript away from the C/Java syntax and into the syntax of Ruby or Python (which is great, because JavaScript is a lot closer to Ruby or Python than it is to C or Java).

Recently I have written a blog post about CoffeeScript that you should read if you want more details about CoffeeScript.

Let me just show you how a web-server looks like in CoffeScript (powered by node.js):

http = require 'http'

[host, port] = ['', 8080]

http.createServer((req, res) ->
    res.writeHead 200, 'Content-Type': 'text/plain'
    res.end 'Hello World\n'
).listen(port, host)

console.log "Server running at http://#{ host }:#{ port }/"

As you can see it's not that much different from the JavaScript version, but I really love that all the noise is removed and only the essentials are left.

Using CoffeeScript is of course optional as it just compiles to JavaScript. But I like to use it because it makes me more productive and makes it possible to write more beautiful code.

npm: Easy to use package manager

npm is node's package manager. It makes it trivial to install and distribute node libraries and programs.

Installing npm is an one-liner:

curl http://npmjs.org/install.sh | sh

Now that you have npm you can install packages with it... For example to install coffee-watcher you simply type:

sudo npm install coffee-watcher

And you have coffee-watcher installed!

To use npm in your node.js program you simply create a package.json file, similar to this one:

    "name":         "coffee-watcher",
    "description":  "A script that can watch a directory ...",
    "keywords":     ["coffeescript", "reload", "watch", "recompile"],
    "author":       "amix",
    "version":      "1.0.0",
    "licenses": [{
        "type":       "MIT",
        "url":        "http://opensource.org/licenses/mit-license.php"

To publish your application and make it available for other developers you simply do:

sudo npm publish

It's simple, great and npm is getting widely popular in the node.js community. You can browse through npm packages here.

Docco documentation

Docco produces HTML documentation that displays comments alongside code. It's a great way to do Donald Knuth's literate programming.

Check out following examples to get an idea what Docco is about:

I think Docco is a refreshing and beautiful way to write and read code.

The future of JavaScript

I think JavaScript is the language of the future for both frontend and backend development. It's easy to see why if following things are taken into consideration:

  • JavaScript is already widely used on the frontend
  • JavaScript is getting more used on mobile platforms with initiative such as jQuery Mobile or PhoneGap. Most mobile platforms also have amazing JavaScript implementations
  • the performance of V8 (and other JavaScript VMs) make JavaScript one of the fastest languages available today
  • a framework like node.js makes it possible to build realtime web-applications that can scale to millions of concurrent users
  • an extension language like CoffeeScript makes JavaScript clear, beautiful and very expressive
  • a thriving community centered around node.js is on the uprising - - and this uprising has just started
  • etc.

2011 could be the year of JavaScript... A year where JavaScript becomes widely popular on frontend, backend and mobile. Or at least I hope so, because I really like JavaScript's stack and the hackers that are pushing all these projects forward.

As always happy hacking... I hope you enjoyed this post.

10. Mar 2011 Code · Code improvement · JavaScript
© Amir Salihefendic