Benchmark: RESTful Service Performance of Node.js vs. JAX-RS

Over the past few months, I’ve read more and more about Node.js. Besides its simplicity in creating a web application’s server part, the superior performance of Node.js was stressed very often.

I decided to run a little benchmark.

The scenario: A RESTful web service that receives the first letter of a city, looks up all cities beginning with the supplied letter in a MySQL database, and returns the cities’ names and IDs using JSON.

The setup:

  • Test system: Intel i5 M540, 4 GB RAM, Windows 7 Professional x64 SP1
  • MySQL 5.5.27 x64 running on localhost
  • Apache JMeter 2.7 running on localhost
  • Node.js 0.8.6 with node-mysql 0.9.6
  • JBoss 7.1.1, JAX-RS, JPA/Hibernate
  • JMeter settings: 50 threads, 500 repetitions (thus, 25.000 requests in total).

Java Code:

@Stateless
@Path("/test")
public class TestService {
    @PersistenceContext
    private EntityManager entityManager;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    public Response action(@QueryParam("beginning") @DefaultValue("") String beginning) {
        Response response = new Response();
        Query query = getEntityManager().createQuery("FROM Name WHERE name LIKE :name");
        query.setParameter("name", beginning + "%");
        response.setNames((List)query.getResultList());
        return response;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}

Node.js Code:

var http = require('http');
var url = require('url');
var mysql = require('mysql');

var client = mysql.createClient({
    user: 'root',
    password: ''
});
client.query('USE `rest-test`');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    var url_parts = url.parse(req.url, true);
    var query = url_parts.query;
    var beginning = query['beginning'];
    client.query('SELECT * FROM name WHERE name LIKE "'+beginning+'%"', function selectCb(error, results, fields) {
        var result = [];
        for (var i=0; i < results.length; i++) {
            var row = results[i];
            result.push({'id': row['id'], 'name': row['name']});
        }
        res.end(JSON.stringify({'names': result}));
    });
}).listen(8080, '127.0.0.1');

The result:

  • Node.js: 148.0 requests per second
  • JAX-RS: 467.2 requests per second

Download the source codes, JMeter test case, and benchmark summary

4 thoughts on “Benchmark: RESTful Service Performance of Node.js vs. JAX-RS”

  1. You are not comparing the same things:
    1. The Java version is protected against SQL injections while the Node.js version is not.
    2. The Java version uses a generic JSON serializer while the Node.js version uses static JSON generation for the elements.
    However, even with these differences, it is interesting to see a 3x performance difference…

Comments are closed.