Improving latency

Improving latency is a large topic that requires a rethink on many of the traditional ways of doing things. For example, it is better not to load non-essential text such as help text with the initial page; better is to load it via AJAX only if the user clicks on the help button. Similarly, those used to Google's Maps API will often load all features together with all descriptive text even if there are a large number of these and the user is only interested in a few; better is to load only what the user requests - as above - and to only load descriptive text when requested, via AJAX. In general, as much logic as possible should be done on the server not the browser; not only is the browser slower but each piece of code has to be transmitted to each browser before it can be executed. Variables calculated on the server can be passed to the browser via a JS object - a namespace, which I call ServerVars - in a script tag.

A final small tip that can make a surprising difference to the size of your code. If you use the OpenLayers classes a lot, you will find that you keep repeating 'OpenLayers' (see code above as example). If you set up a shorter alias for this, for example

    var OL = OpenLayers;

at the top of your code, you can then refer to this throughout, for example

    new OL.Strategy.Fixed()

You may be surprised how many bytes this can save. :-)

原文地址:https://www.cnblogs.com/ethelhao/p/3584103.html