Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
bcrypt.compare() or bcrypt.compareSync()

I have a question regarding this topic: bcrypt.compare() is asynchronous, does that necessarily mean that delays are certain to happen?

Since I'm not allowed to put comments because of my membership level I had to open new topic.

My question is what are the downsides or is there any for using bcrypt.compareSync() instead of the async version of bcrypt.compare().

compareSync() definitely gives the correct result. So why not use it and use the compare() wrapped in Promises? Is it going to halt the nodeJS from serving other users?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The reason to use the async methods instead of the sync ones are explained in the readme of the project quite well.

Why is async mode recommended over sync mode?

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. The async version uses a thread pool which does not block the main event loop.

https://github.com/kelektiv/node.bcrypt.js#why-is-async-mode-recommended-over-sync-mode

So if you are using this in a webapplication or other environment where you don't want to block the main thread you should use the async version.

about 4 years ago · Juan Pablo Isaza Report

0

Node.js native methods have Sync attached methods like fs.writeFileSync, crypto.hkdfSync, child_process.execSync. JavaScript in the browser is implemented asynchronously with all native functions that require thread blocking, but Sync methods in Node.js actually block threads until the task is complete.

When using Callback or Promise in Node.js, if only asynchronous logic is executed internally, it becomes possible to manage asynchronous tasks while proceeding with other tasks without stopping the main thread (using count for Callbak, Promise.all).

Sync method runs the next line after work, so it is easy to identify the order of execution and easy to code. However, the main thread is blocked, so you can't do more than one task at a time.

Think about the next example.

const syncFunc = () => {
  for (let i = 0; i < 100; i++) fs.readFileSync(`/files/${i}.txt`);
  console.log('sync done');
};

const promiseFunc = async () => {
  await Promise.all(Array.from({length: 100}, (_,i) => fs.promises.readFile(`/files/${i}.txt`)));
  console.log('promise done');
};

The promise function ends much faster when there is no problem reading all 100 txt files.

This Sync feature applies equally to libraries made of C language. If you look at the following code, you can see the difference in implementation in C++.

  • compare
  • compareSync

In conclusion, I think it's a matter of choice. There is no problem using Sync method if the code you make is logic that goes on a single thread that doesn't matter if the main thread is blocked(like simple macro). However, if you are making logic where performance issues such as servers are important and the main thread should not stop as much as possible for thread or asynchronous management, you can choose Promise or Callback.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!