I have a simulator that pulls data from a DB – calculates and return JSON result to an ajax call that renders a table for the results.
The calculation procedure are as follow:
- grab X number of data that are group together and have weights to
them. - use historical data and run a weight distribution algorithm on the
group. - return as a JSON the metrics of each data, the old value, and the
calculated new value
2 Questions:
1) Is Javascript calculation better or worse (or equal) to PHP calculation in order to yield the results.
2) IF Javascript calculation is faster or on par to PHP then I would assume the trip to call ajax is a small bottleneck. So one possibility would be to pre-load data and have it calculated on the fly. Now how would you persist that pre-loading efficiently? Would that just simply load everything in a simple var via ajax?
Thus far it takes approx 55 seconds for the result to return 2000 entries on 14 days worth of data.
3
You are correct you need to optimize your code somehow.
However, the first rule of code optimization is to
Measure, don’t guess.
There very likely are performance differences between Javascript on a desktop and PHP on a server, but those differences aren’t big enough to solve your problem.
If you are using Chrome as your browser, press F12 and pay attention to the Network section. It also helps to heavily use debugger;
, console.time('foo');
and console.timeEnd('foo');
in your javascript code.
Your first question should be to determine if the server is slow in getting the data to the client or if the client is slow in displaying the data.
You can’t optimize code until you know for sure which code needs optimizing.
1