pbojinov
4/27/2015 - 8:26 PM

gistfile1.txt

var square = (function() {
	'use strict';

	var funcMemoized = function() {
		var cacheKey = JSON.stringify(Array.prototype.slice.call(arguments));
		var result;

		if (!funcMemoized.cache[cacheKey]) {
				// your expensive computation goes here
				// to reference the paramaters passed, use arguments[n]
				// eg.: result = arguments[0] + arguments[1];
				console.log('computing for the first time');
				result = arguments[0] * arguments[0];
				funcMemoized.cache[cacheKey] = result;
		} else {
	        console.log('returning from cache');
		}

		return funcMemoized.cache[cacheKey];
	};

	funcMemoized.cache = {};

	return funcMemoized;
}());