Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

memoize

oatkiller edited this page Sep 13, 2010 · 2 revisions

memoizing is an old concept. google it

(function () {

// this will cache the returns from the fn in my_cached_ajax_responses

var my_cached_ajax_responses = {},
get_data_by_id_and_type = function (id,type) {
	// do an ajax call
	return my_data; // something you got from ajax
}[o.memoize](my_cached_ajax_responses);

// does the ajax call
get_data_by_id_and_type(1,'name'); 

// no ajax call, you get the cached version
get_data_by_id_and_type(1,'name');

// the cached version is stored at the join of the arguments
var key = [1,'name'].join(); // '1,name'

// delete the cached version
delete my_cached_ajax_responses[key];

// the ajax call happens again
get_data_by_id_and_type(1,'name');

})();
Clone this wiki locally