Javascript Memoisation / Memoization
This is more an excuse to create a jsfiddle but I wanted to play with memoisation / memoization (depending on your location).
http://jsfiddle.net/kushalj/8Xmfv/1/
This is just Crockford's example from 'Javascript the Good Parts' book. Working and playing helped me solidify the concept.
A very cool caching system.
var count = 0; var fibonacci = function (n) { count++ return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2) }; for (var i = 0; i <= 10; i += 1) { console.log('// ' + i + ': ' + fibonacci(i) ); }; console.log("fibonacci called " + count + " times"); var fibonacci2 = (function ( ) { var memo = [0, 1]; var fib = function (n) { var result = memo[n]; if (typeof result !== 'number') { result = fib(n − 1) + fib(n − 2); memo[n] = result; } return result; }; return fib; }( ));













