Interesting JavaScript Optimization Pattern
            June 27. 2009
            
                
                0 Comments
            
                    
    - Posted in:
 - javascript
 
Bumped into this a while back, but haven’t got a chance to use it so I sort of forgot about it. It’s been floating around in the back of my mind. I found this one pattern to be quite clever in helping with JavaScript function optimizations, especially when doing browser specific code and some other code that might only require a one time hit and sort of cache the result in the function afterward.
The code is as follow:
function A() {    
    //Do some initial calculation of some sort...    
    var returnValue = GetValueFromWebService("...");
    //Rewrite A to actually return the calculated returnValue    
    //and stop going to the web service ever again    
    //In essence, caching the inital returnValue for subsequent calls    
    A = function()  {
        return returnValue;   
    };
    return A();
}
Very neat
