BlueChips23
BlueChips23

Reputation: 1951

Node.js: offloading some JavaScript processing onto the server

I am new to Node.js and I am still learning it as I try to attempt to solve a problem. However, I am stuck and need some input.

I have a website (for simplicity sake, let's call it MyMathPage.html) which does some mathematical calculations on the client side (using JavaScript) and displays the results.

So, the header of the MyMathPage.html looks somewhat like this:

<!DOCTYPE html> 
  <html> 
    <head> 
      <link rel='stylesheet' type='text/css' href='../css/style.css'> 
      <script type='text/javascript' src='../js/jquery.js'></script>
      <script type='text/javascript' src='../js/Maths.js'></script>
      <script type='text/javascript' src='../js/GetAnswer.js'></script>
      ...

However, some of the mathematical functions take too long to solve (~15-20 seconds), so I am experimenting with Node.js to see if the processing within Maths.js can be done on the server side (on Node.js) to speed up the calculations a bit more, as the browser clients typically don't have a lot of memory or processing power.

My idea of the new architecture is this:

  1. GetAnswer.js calls var math = new Maths(calculationToMake, input) from the browser side
  2. On Node.js, I will have Maths.js which will accept the calculationToMake and input from the request, does all the calculation server side and returns a response (return MathsAnswer - of return type Maths)
  3. GetAnswer.js receives the response and displays the result.

My question is, if I take Maths.js file into node.js server side, how will my request for "var math = new Maths(var1, var2) )" from GetAnswer.js will look like? Also how will my response on the Node.js will look like?

Upvotes: 0

Views: 895

Answers (1)

dlongley
dlongley

Reputation: 2096

Node.js is probably the wrong tool for this; it is single-threaded (event-based) and you'll essentially prevent the server from serving anyone else while it does this calculation. In other words, if you do the math on the client, each client may have to wait 20 seconds for a result. However, the clients can all run in parallel. If you do this math on the server, perhaps it will be faster, but each client will have to wait in line for other clients to get their results first. It's likely that you'd have to really complicate the math code to avoid this.

It's questionable how much faster having a single server do this work would really be -- especially as the number of clients increases. Keep in mind that every new concurrent client essentially cuts the computing resources of the server. IOW, if your server is twice as fast as a typical client and you have three typical concurrent clients, the system is already slower than it was before (clients are waiting longer).

Instead, perhaps the easiest thing to do would be to use a Web Worker and show a progress bar. This won't speed things up, obviously, but it will make the experience a little better. Other alternatives involve writing native code to really speed things up or trying to run separate processes/threads in node.js. However, these options seem like over-engineering an otherwise simple problem (again, using the wrong tool for the job).

There are a variety of ways you can get long-running CPU-intensive code working in a sensible way with node.js, but it doesn't sound like it's the right solution here. It sounds like either you shouldn't be doing this math in JavaScript -- or you need some better algorithms to speed it up. That ... or just a better UI experience so people are ok with waiting.

Upvotes: 2

Related Questions