Accenture Interview Question

Javascript memorization

Interview Answer

Anonymous

Apr 8, 2014

memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs You'd normally use memoization to reduce the cost of repeatedly computing a result that will always be the same. Any performance improvement comes at the expense of allocating memory for the cached results. A simple example in code: var cachedResult; function doHeavyCalculation() { if (typeof(cachedResult) !== 'undefined') return cachedResult; // no cached result available. calculate it, and store it. cachedResult = /* do your computation */; return cachedResult; } There are JavaScript frameworks that support memoizing any function, and they basically provide this boilerplate code for you in a reusable fashion by decorating a function.

1