Nirav Solanki
Nirav Solanki

Reputation: 89

fs.watch vs setInterval in node.js

I have application where i am reading data from csv file at every interval of 500ms.

CSV file is changed at every 300ms from another desktop based application.

So which one is better to use fs.watch or setInterval in this case.

Upvotes: 1

Views: 626

Answers (1)

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

In this situation I'l go with fs.watch it is helping me to create a more robust architecture.

Let's assume we are using timers setTimeout|setInterval, we need to hardcode the delay, and meanwhile the front application is scaling up and is updating the csv faster or slower, then you will need to modify your code so using fs.watch you just don't care how many change events occured, your application will not need any changes.

The biggest issue that I see at the moment with fs.watch is if the front will update the csv so fast that you will not finish your import and a new event will be dispatched then you will have hard time to deal with race conditions, but till that moment fs.watch is a good call imo.

Upvotes: 1

Related Questions