-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (134 loc) · 3.8 KB
/
index.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
var cacheSymbol = Symbol();
var hasResult = Symbol();
var result = Symbol();
var objectsCache = Symbol();
var limitedObject = Symbol();
var primitivesKeysQueue = Symbol();
var limit = Symbol();
var noResult = Symbol();
function mapHas(map, key) {
if (typeof key !== 'object') {
return map.hasOwnProperty(key);
} else {
if (map[objectsCache]) {
return map[objectsCache].has(key);
} else {
return false;
}
}
}
function mapGet(map, key) {
if (typeof key !== 'object') {
return map[key];
} else {
return map[objectsCache].get(key);
}
}
function mapSet(map, key, value) {
if (typeof key !== 'object') {
if (map[limitedObject]) {
var queue = map[primitivesKeysQueue];
if (queue.length >= map[limit]) {
delete map[queue.shift()];
}
queue.push(key);
}
map[key] = value;
} else {
if (!map[objectsCache]) {
map[objectsCache] = new WeakMap();
}
map[objectsCache].set(key, value);
}
}
function createCombinedMap(primitivesLimit) {
var combinedMap = {};
combinedMap[cacheSymbol] = true;
if (typeof primitivesLimit === 'number') {
combinedMap[limitedObject] = true;
combinedMap[primitivesKeysQueue] = [];
combinedMap[limit] = primitivesLimit;
}
return combinedMap;
}
function get(cache, params, remainingParamsLength) {
if (remainingParamsLength === 0) {
return cache[result];
}
var currentParamsKey = params[params.length - remainingParamsLength];
var hasKey = mapHas(cache, currentParamsKey);
if (hasKey) {
var keyValue = mapGet(cache, currentParamsKey);
if (remainingParamsLength === 1) {
if (typeof keyValue === 'object' && keyValue[cacheSymbol]) {
return keyValue[result];
} else {
return keyValue;
}
}
return get(keyValue, params, remainingParamsLength - 1);
}
return noResult;
}
function set(value, cache, cacheLimit, params, remainingParamsLength) {
if (remainingParamsLength === 0) {
cache[hasResult] = true;
cache[result] = value;
return;
}
var currentParamsKey = params[params.length - remainingParamsLength];
var hasKey = mapHas(cache, currentParamsKey);
var nextCache;
if (remainingParamsLength === 1) {
if (hasKey) {
nextCache = mapGet(cache, currentParamsKey);
nextCache[hasResult] = true;
nextCache[result] = value;
return;
}
else {
mapSet(cache, currentParamsKey, value);
}
return;
}
if (hasKey) {
var keyValue = mapGet(cache, currentParamsKey);
if (typeof keyValue === 'object' && keyValue[cacheSymbol]) {
set(value, keyValue, cacheLimit, params, remainingParamsLength - 1);
}
else {
nextCache = createCombinedMap(cacheLimit);
nextCache[hasResult] = true;
nextCache[result] = keyValue;
mapSet(cache, currentParamsKey, nextCache);
set(value, nextCache, cacheLimit, params, remainingParamsLength - 1);
}
return;
}
nextCache = createCombinedMap(cacheLimit);
mapSet(cache, currentParamsKey, nextCache);
set(value, nextCache, cacheLimit, params, remainingParamsLength - 1);
}
function memoize(fn, options) {
var primitivesCacheLimit = options && options.primitivesCacheLimit;
var cache = createCombinedMap(primitivesCacheLimit);
function memoizedFn() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var possibleResult = get(cache, args, _len);
if (possibleResult === noResult) {
var result = fn.apply(undefined, args);
set(result, cache, primitivesCacheLimit, args, _len);
return result;
} else {
return possibleResult;
}
}
return memoizedFn;
}
module.exports = memoize;
module.exports.memoize = memoize;
module.exports.default = memoize;
module.exports.__esModule = true;