How to make koa.js faster by 47 percent in 3 lines of code

Jiří Špác
2 min readJun 6, 2016

--

So there I was, typical Sunday night-just browsing the interwebs avoiding the unavoidable-doing actual work. I usually hit up twitter and reddit. This evening reddit delivered and gave me a reason do a little bit of what you hipsters call “hacking”.

Looking at the readme for co-bluebird I think to myself-hey my favourite http framework for node.js is basically built on co. Wrapping your handlers with co is one of the most important things it does. So if bluebird-co can be as fast as these benchmarks claim, it could be very beneficial for koa.

So a git clone later and wrk compile later I am running koa.js benchmarks with vanilla co. Results are as followed:

Then I quickly tap into all the hard work done by @PetkaAntonov. I get much nicer results:

This is the beauty of npm and node.js ecosystem-you can do so much just by leveraging other people’s OSS work, applying it in the right context.
I don’t know how my PR is gonna go, but I sure hope koa embraces bluebird. It will take couple of years before native promises catch up with bluebird’s performance-I shun to think how many server hours could be spent in vain if koa doesn’t embrace bluebird.

Anyway-thanks to Aaron Trent for making bluebird-co and to you my dear reader I’d like to say: keep hacking. Who knows, maybe one day, we will make node.js trully webscale :D

EDIT: Turns out that to get the same boost for both async functions and generators, all you have to do is:

global.Promise = require(‘bluebird’)

before you require koa, no need to change from battle-tested co to bluebird-co. See my comment for bench results for that.

--

--

Jiří Špác

Node.js/Javascript fullstack enthusiast, living in Brno, CZ, in love with OSS, quality code and his two girls.