JS can also cache data to speed up processing. It is worthwhile to use space for time when necessary. For example, it takes a long time to complete the calculation, you can cache the results to the client, you can directly use the cached results, not to repeat the calculation.
1 caching of the computed results of a simple function 2 recursive function of the cached 3Ajax read data
1 caching of computed results for simple functions
For example:
The common function, encapsulating internal calls, caches the result function memorize (f) { var cache={}; return function () { var key=arguments.length+array.prototype.join.call (arguments, ",");//parameter length and parameter as attribute value if ( Key in cache)//presence directly reads cache data { return cache[key]; } else { return cache[key]=f.apply (this,arguments);//does not exist, then calculates and caches the result of the calculation}} }
Specific use
Simulates a function testfunction (x) {return x*x, which takes a long time to calculate ;} Test, call function test () { var t=memorize (testfunction); var k=t (6);//The first call needs to calculate var k2=t (6);//The second time to read the buffer directly, not to calculate alert (K2); }
2
caching of computed results for recursive functions
Specific use
function Test2 () { var t=memorize (function (x) {///recursive function computes the factorial if of x (x<=0) {return 1;} else{return X*t (x-1)} ); var k=t (6);//cache 6 to 1 factorial var k2=t (7);//Only one call is required, and the cache alert (K2) is used to calculate 6 factorial ; }
3Ajax Cache of Read Data
The Ajax cache can also be used in a similar way, but it's still learned from Https://github.com/devbridge/jQuery-Autocomplete.
The principle is actually the same as the top.
The core sections are organized as follows:
function Autocomplete () {this.cachedresponse = {};//cached data saved container this.badqueries = [after];//query, there are no queries that meet the criteria} autocom Plete.prototype.clearCache = function ()//empty Cache {this.cachedresponse = {}; This.badqueries = []; }, Autocomplete.prototype.getsuggestions=function (q) {//Read data var response, that = this, Options = that.options, serviceurl = Options.serviceurl, data, cache Key; Options.params[options.paramname] = q; data = Options.ignoreparams? Null:options.params; CacheKey = serviceurl + '? ' + $.param (Data | | {});//The cached key is a URL and the passed parameter consists of response = that.cachedresponse[cachekey];//Gets the cached data if (response && $. IsArray (response.suggestions)) {//If there is a cache that.suggestions = response.suggestions; That.suggest (); } else if (!that.isbadquery (q)) {//has not been queried, and is not a query that does not meet the criteria after the query That.currentrequest = $.ajax ({//no cache reads data through Ajax Url:serviceurl, Data:data, Type:options.type, DataType:options.dataType}). Done (function (data ) {that.currentrequest = null; That.processresponse (data, q, CacheKey);//Cache Options.onSearchComplete.call (that.element, q); }). Fail (function (JQXHR, textstatus, Errorthrown) {options.onSearchError.call (that.element, Q, JQXHR, Textstatus, Errorthrown); }); } }
You can see the core is the same as the above, but the key to the cache data becomes the Ajax URL and the parameters passed