最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bcrypt.compare() or bcrypt.compareSync() - Stack Overflow

programmeradmin1浏览0评论

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

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

My question is what are the downsides or is there any for using bcryptpareSync() instead of the async version of bcryptpare().

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

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

Since I'm not allowed to put ments 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.pareSync() instead of the async version of bcrypt.pare().

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

Share Improve this question asked Sep 25, 2021 at 7:49 damdafaytondamdafayton 2,4894 gold badges13 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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 remended 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 remended. 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./kelektiv/node.bcrypt.js#why-is-async-mode-remended-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.

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 plete.

When using Callback or Promise in Node.js, if only asynchronous logic is executed internally, it bees 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++.

  • pare
  • pareSync

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.

发布评论

评论列表(0)

  1. 暂无评论