I'm writing a node rest backend with Restify and I was wondering about async vs. synchronous functions. Particularly about bcrypt. I found this on node-bcrypt:
If you are using bcrypt on a simple script, using the sync mode is perfectly fine. However, if you are using bcrypt on a server, the async mode is recommended. This is because the hashing done by bcrypt is CPU intensive, so the sync version will block the event loop and prevent your application from servicing any other inbound requests or events.
So let's take Restify as an example, if I have something like this:
server.put('/user', function(req, res, next) {
var hash = bcrypt.hashSync('qwerty', 10);
});
This would block all other requests until the hash is generated, right?
But what if I wrapped everything in a timer?
server.put('/user', function(req, res, next) {
setTimeout(function() {
var hash = bcrypt.hashSync('qwerty', 10);
}, 0);
});
Now it doesn't block anything, does it? The reason I ask is, because I have an endpoint that updates settings and user data such as email, password. So it would be much simpler, if I could use the sync function, otherwise, the code gets more complicated.
via Maciej Krawczyk
No comments:
Post a Comment