memoize.js
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function(factory){
if(typeof define != "undefined"){
define([], factory);
}else if(typeof module != "undefined"){
module.exports = factory();
}else{
dclAdvicesMemoize = factory();
}
})(function(){
"use strict";
return {
advice: function(name, keyMaker){
return keyMaker ?
{
around: function(sup){
return function(){
var key = keyMaker(this, arguments), cache = this.__memoizerCache, dict;
if(!cache){
cache = this.__memoizerCache = {};
}
if(cache.hasOwnProperty(name)){
dict = cache[name];
}else{
dict = cache[name] = {};
}
if(dict.hasOwnProperty(key)){
return dict[key];
}
return dict[key] = sup ? sup.apply(this, arguments) : undefined;
}
}
} :
{
around: function(sup){
return function(first){
var cache = this.__memoizerCache, dict;
if(!cache){
cache = this.__memoizerCache = {};
}
if(cache.hasOwnProperty(name)){
dict = cache[name];
}else{
dict = cache[name] = {};
}
if(dict.hasOwnProperty(first)){
return dict[first];
}
return dict[first] = sup ? sup.apply(this, arguments) : undefined;
}
}
};
},
guard: function(name){
return {
after: function(){
var cache = this.__memoizerCache;
if(cache && name){
delete cache[name]
}else{
this.__memoizerCache = {};
}
}
};
}
};
});