Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda