Bcrypt vs BcryptJS Benchmark with Node.js

Petros Koulianos
Mar 16 · 3 min read
Photo by Marc Sendra Martorell on Unsplash

1.Introduction

Bcrypt is among the most popular and safest, one-way hashing functions for passwords.

For Node.js applications, we have 2 options bcrypt and bcryptjs.

bcrypt is written in C++ with more than 400.000 downloads per week at npm and 5.1k stars at github.

bcryptJS is written in Javascript with more than 560.000 downloads per week at npm and 2.3k stars at github.

We’ll try to benchmark both libraries at:

  • Generate Hash password synchronous
  • Compare Hash password asynchronous
  • Generate hash password synchronous
  • Compare hash password asynchronous

All tests are made at Digital Ocean standard droplet with:

1vCPU and 1GB memory

2.Generate and Compare hash passwords

The recommended way to generate and compare passwords is to use async functions, but in this benchmark, we’ll test both async and sync ways.

mkdir bcryptBench
cd bcryptBench
npm init -y
npm i bcrypt bcryptjs

We’ll test bcrypt lib, create a new file index.js and paste the following code

const bcrypt = require('bcrypt');const saltRounds = 8;const myPlaintextPassword = 'Mystr0ngP@SSW0RD';var hashAsync;var hashSync;console.time('bcrypt_Async Generate Hash');bcrypt.hash(myPlaintextPassword, saltRounds).then(function (hash) {hashAsync = hash;console.timeEnd('bcrypt_Async Generate Hash');console.time('bcrypt_Async Compare Hash');bcrypt.compare(myPlaintextPassword, hashAsync).then(function (result) {console.timeEnd('bcrypt_Async Compare Hash');});});console.time('bcrypt_Sync Generate Hash');hashSync = bcrypt.hashSync(myPlaintextPassword, saltRounds);console.timeEnd('bcrypt_Sync Generate Hash')console.time('bcrypt_Sync Compare Hash');bcrypt.compareSync(myPlaintextPassword, hashSync);console.timeEnd('bcrypt_Sync Compare Hash');

Run the code

node index.js
Example output for bcrypt

We’ll test bcryptjs lib, create a new file index2.js and paste the following code

var bcryptjs = require('bcryptjs');const saltRounds = 8;const myPlaintextPassword = 'Mystr0ngP@SSW0RD';var hashAsync;var hashSync;console.time('bcryptjs_Async Generate Hash');bcryptjs.hash(myPlaintextPassword, saltRounds).then(function (hash) {hashAsync = hash;console.timeEnd('bcryptjs_Async Generate Hash')console.time('bcryptjs_Async Compare Hash');bcryptjs.compare(myPlaintextPassword, hashAsync).then(function (result) {console.timeEnd('bcryptjs_Async Compare Hash');});});console.time('bcryptjs_Sync Generate Hash');hashSync = bcryptjs.hashSync(myPlaintextPassword, saltRounds);console.timeEnd('bcryptjs_Sync Generate Hash')console.time('bcryptjs_Sync Compare Hash');bcryptjs.compareSync(myPlaintextPassword, hashSync);console.timeEnd('bcryptjs_Sync Compare Hash');

Run the code

node index2.js
Example output for bcryptjs

3. Numbers

Bar chart with results from the above test. The lower time, the better performance

bcrypt vs bcryptjs bar chart

Async functions

Bcrypt is 1.8 times faster than bcryptjs in generating hash passwords and 1.8 times faster in comparing function.

Sync functions

Bcrypt is 3.1 times faster than bcryptjs in generating hash passwords and 1.3 times faster in comparing function.

3.Conclusion

Bcrypt is way faster than bcryptjs, although users prefer bcryptjs at npm.
Both libraries are excellent and easy to accomplish their purpose.

References

Thanks for reading my story

Feel free to comment me for any ideas, changes, etc.

JavaScript in Plain English

Learn the web's most important programming language.

Petros Koulianos

Written by

Passionate Developer with main interests at node.js and java, marathoner, mountaineering. petranb2@gmail.com

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote

10.4K

More from JavaScript in Plain English

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade