Home > front end >  Regexp affecting performance of nodejs app. How to replace improve Regexp in javascript?
Regexp affecting performance of nodejs app. How to replace improve Regexp in javascript?

Time:10-06

I am developing an electron/nodejs app that uses some regexps, two of them are the following :

str=  str.replace(/\((.*?)\)/, "");
str= str.match(/\d /g);

I am using these regexps inside a function that blocks my electron UI. I then tested the function and I found out that regexps are taking much time.

I researched this issue through SO but alas! JavaScript regexp performance. This did not help me.

Edit: I have thousands of strings to be tested by regexp. which create a very bad impact even blocks the UI of my electron app!

CodePudding user response:

I have done a similar task on log files with hundreds of thousands of entries. Unfortunately Node & Electron are not the best choices for this type of task.

While maybe not ideal, the overall solution I was able to use was to create a small application in golang (albeit it could be any other more efficient language) which accepts a simple command line argument and then returns piped data.

After this I was able to use Node/Electron to spawn the child process and call the golang program and grab the response. I was able to reduce minutes of processing to seconds and still enjoy my wonderful Electron interface.

Sometimes a little creativity goes a long way. There is not a single language which is stellar in everything, sometimes it is best to pull strengths from each.

CodePudding user response:

Unfortunately doing that kind of processing on thousands of strings is probably not likely to be too efficient regardless of what you do.

I don't have any experience with electron, but you might see if it's possible to use web workers to split up the work among multiple threads and get the heavy processing done faster. See Using Web Workers from MDN and the top answer to this SO post for some good info on web workers.

  • Related