Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/memoize/memoize.js
4167 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Tool for caching the result of expensive deterministic
9
* functions.
10
*
11
* @see http://en.wikipedia.org/wiki/Memoization
12
*/
13
14
goog.module('goog.memoize');
15
goog.module.declareLegacyNamespace();
16
17
const reflect = goog.require('goog.reflect');
18
19
/**
20
* Note that when using the WeakMap polyfill users may run into issues
21
* where memoize is unable to store a cache properly (as the polyfill tries to
22
* store the values on the key object as properties.). The workaround is to not
23
* memoize onto a sealed context if the code needs to run in browsers where
24
* WeakMap is not available (IE<=10 as an example).
25
* @type {!WeakMap<!Object, !Object>}
26
*/
27
const MODULE_LOCAL_CACHE = new WeakMap();
28
29
/**
30
* Decorator around functions that caches the inner function's return values.
31
*
32
* To cache parameterless functions, see goog.functions.cacheReturnValue.
33
*
34
* @param {Function} f The function to wrap. Its return value may only depend
35
* on its arguments and 'this' context. There may be further restrictions
36
* on the arguments depending on the capabilities of the serializer used.
37
* @param {function(number, !IArrayLike<?>): string=} serializer A function to
38
* serialize f's arguments. It must have the same signature as
39
* goog.memoize.simpleSerializer. It defaults to that function.
40
* @return {!Function} The wrapped function.
41
*/
42
function memoize(f, serializer = simpleSerializer) {
43
const uidF = goog.getUid(f);
44
const keyFn = ([that, ...args]) => serializer(uidF, args);
45
const valueFn = ([that, ...args]) => f.apply(that, args);
46
47
/**
48
* @this {Object} The object whose function is being wrapped.
49
* @param {...*} args
50
* @return {?} the return value of the original function.
51
*/
52
const memoizedFn = function(...args) {
53
if (memoize.ENABLE_MEMOIZE) {
54
const cacheKey = this || goog.global;
55
let cache = MODULE_LOCAL_CACHE.get(cacheKey);
56
if (!cache) {
57
cache = {};
58
MODULE_LOCAL_CACHE.set(cacheKey, cache);
59
}
60
return reflect.cache(cache, [this, ...args], valueFn, keyFn);
61
} else {
62
return f.apply(this, args);
63
}
64
};
65
return memoizedFn;
66
}
67
exports = memoize;
68
69
/**
70
* @define {boolean} Flag to disable memoization in unit tests.
71
*/
72
memoize.ENABLE_MEMOIZE = goog.define('goog.memoize.ENABLE_MEMOIZE', true);
73
74
75
/**
76
* Clears the memoization cache on the given object.
77
* @param {?Object} cacheOwner The owner of the cache.
78
*/
79
const clearCache = function(cacheOwner) {
80
MODULE_LOCAL_CACHE.set(cacheOwner || goog.global, {});
81
};
82
exports.clearCache = clearCache;
83
84
85
/**
86
* Simple and fast argument serializer function for goog.memoize.
87
* Supports string, number, boolean, null and undefined arguments. Doesn't
88
* support \x0B characters in the strings.
89
* @param {number} functionUid Unique identifier of the function whose result
90
* is cached.
91
* @param {!IArrayLike<?>} args The arguments that the function to memoize is
92
* called with. Note: it is an array-like object, because it supports
93
* indexing and has the length property.
94
* @return {string} The list of arguments with type information concatenated
95
* with the functionUid argument, serialized as \x0B-separated string.
96
*/
97
const simpleSerializer = function(functionUid, args) {
98
const context = [functionUid];
99
for (let i = args.length - 1; i >= 0; --i) {
100
context.push(typeof args[i], args[i]);
101
}
102
return context.join('\x0B');
103
};
104
exports.simpleSerializer = simpleSerializer;
105
106