fs.readFileSync seems faster than fs.readFile - is it OK to use for a web app in production?

前端 未结 5 1051
难免孤独
难免孤独 2020-12-05 13:19

I know that when developing in node, you should always try to avoid blocking (sync) functions and go with async functions, however I a little test to see how they compare.

5条回答
  •  星月不相逢
    2020-12-05 13:46

    You are blocking the callback of the asynchronous read with your synchronous read, remember single thread. Now I understand that the time difference is still amazing, but you should try with a file that is much, much longer to read and imagine that many, many clients will do the same, only then the overhead will pay off. That should answer your question, yes you will run into trouble if you are serving thousands of requests with blocking IO.

提交回复
热议问题