JSPatterns.com » cache

Posts Tagged ‘cache’

Function properties

Tuesday, September 15th, 2009

Since functions are objects, and objects are mutable in JavaScript, you can add properties to your functions.

Why? For caching results of computations for example. Here's a memoization done this way.

function myFunc(param){
    if (!myFunc.cache) {
        myFunc.cache = {};
    }
    if (!myFunc.cache[param]) {
        var result = {}; // ...
        myFunc.cache[param] = result;
    }
    return myFunc.cache[param];
}

Here myFunc() gets a cache property which is an object. The function does the complicated computations based on a parameter and ends up with a result object. Using the parameter as a key in the cache object, it stores the result.

Consecutive calls with the same parameter will get the result from the cache, no need to compute the same result again.


hacklink panel bypass shell wso shell evden eve nakliyat hacklink satış hacklink paykasa hacklink hacklink satış hacklink panel hacklink al hacklink satis hacklink hacklink evden eve nakliyat istanbul evden eve nakliyat nakliyat nakliye evden eve nakliyat istanbul evden eve nakliyat nakliyat nakliye depolama Google film izle porno porno izle porno seyret seks hikayeleri Google Evden Eve Nakliyat İstanbul Evden Eve Nakliyat Nakliyat İstanbul Nakliyat istanbul Evden Eve Nakliyat Nakliye Firmaları Şehirler Arası Evden Eve Nakliyat