Header Image One of Node’s greatest features is being able to embed C++ code and use it in other JavaScript files. However with the rising popularity of Node (and thus JavaScript) people have been starting to rewrite large libraries like zlib in JavaScript. This is very cool to see, it’s not quite as cool when you look at the performance of these libraries.

To show this, I’m going to use js-deflate and Node’s built in zlib module. You can see the code I used to do this here. What it does is create 3 differently sized strings (500, 50,000, 100,000). Then it times them being deflated through raw JavaScript, and the built in C++ zlib bindings. Now for the results:

Deflating smallest string using zlib.. Took 188ms (avg 0.188ms)
Deflating medium string using zlib.. Took 1944ms (avg 1.944 ms)
Deflating largest string using zlib.. Took 4679ms (avg 4.679ms)
Deflating smallest string using javascript.. Took 185ms (avg 0.185ms)
Deflating medium string using javascript.. Took 11903ms (avg 11.903ms)
Deflating largest string using javascript.. Took 25996ms (avg 25.997ms)

As you can see, zlib and JavaScript compressed our 500 character string in nearly the same time.. with JavaScript being faster! However it’s not quite as impressive once you look at the bigger strings. JavaScript took 6.1 times as long to compress the medium string and 5.5 times as long to compress the largest string.

So obviously you should be using the C++ libraries for this kind of work when you can. However, what happens when you can’t? I had a client who simply could not install node.bcrypt.js (which despite the name, is not coded in JavaScript) and told me I had to use something else. So I found the pure JavaScript bcrypt.js library. All was good, until I realized that this JavaScript library was just as slow as js-deflate compared to their C++ counterparts. This wouldn’t be an issue, however there is no way to actually run it async.

So I created a package for it. You can find it here. It’s name is requireAsync, because you use it the exact same way you’re use require in NodeJS. So first off, you have to require (synchronously!) require-async:

var requireAsync = require('require-async');

Then, you can use requireAsync to include another module or file:

var bcrypt = requireAsync('bcryptjs');

After that, you can nearly run the functions the same (this bcrypt library had these fake “async” methods, so I had to use the “sync” methods):

bcrypt('hashSync', function(err, salt) {
    if (err) {
        throw err;
}, 'bacon', 8);

So if you tried out this example, it would run hashSync in a different process, and give me a bcrypt hash for ‘bacon’.

PS: requireAsync was just recoded in TypeScript, so no need to write definitions for it!