Debian Codegraph @

closure-library/closure/goog/memoize/memoize.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Tool for caching the result of expensive deterministic
17 * functions.
18 *
19 * @see http://en.wikipedia.org/wiki/Memoization
20 *
21 */
22
23 goog.provide('goog.memoize');
24
25
26 /**
27 * Decorator around functions that caches the inner function's return values.
28 *
29 * To cache parameterless functions, see goog.functions.cacheReturnValue.
30 *
31 * @param {Function} f The function to wrap. Its return value may only depend
32 * on its arguments and 'this' context. There may be further restrictions
33 * on the arguments depending on the capabilities of the serializer used.
34 * @param {function(number, Object): string=} opt_serializer A function to
35 * serialize f's arguments. It must have the same signature as
36 * goog.memoize.simpleSerializer. It defaults to that function.
37 * @this {Object} The object whose function is being wrapped.
38 * @return {!Function} The wrapped function.
39 */
40 goog.memoize = function(f, opt_serializer) {
41 var serializer = opt_serializer || goog.memoize.simpleSerializer;
42
43 return function() {
44 if (goog.memoize.ENABLE_MEMOIZE) {
45 // In the strict mode, when this function is called as a global function,
46 // the value of 'this' is undefined instead of a global object. See:
47 // https://developer.mozilla.org/en/JavaScript/Strict_mode
48 var thisOrGlobal = this || goog.global;
49 // Maps the serialized list of args to the corresponding return value.
50 var cache = thisOrGlobal[goog.memoize.CACHE_PROPERTY_] ||
51 (thisOrGlobal[goog.memoize.CACHE_PROPERTY_] = {});
52 var key = serializer(goog.getUid(f), arguments);
53 return cache.hasOwnProperty(key) ?
54 cache[key] :
55 (cache[key] = f.apply(this, arguments));
56 } else {
57 return f.apply(this, arguments);
58 }
59 };
60 };
61
62
63 /**
64 * @define {boolean} Flag to disable memoization in unit tests.
65 */
66 goog.define('goog.memoize.ENABLE_MEMOIZE', true);
67
68
69 /**
70 * Clears the memoization cache on the given object.
71 * @param {Object} cacheOwner The owner of the cache. This is the {@code this}
72 * context of the memoized function.
73 */
74 goog.memoize.clearCache = function(cacheOwner) {
75 cacheOwner[goog.memoize.CACHE_PROPERTY_] = {};
76 };
77
78
79 /**
80 * Name of the property used by goog.memoize as cache.
81 * @type {string}
82 * @private
83 */
84 goog.memoize.CACHE_PROPERTY_ = 'closure_memoize_cache_';
85
86
87 /**
88 * Simple and fast argument serializer function for goog.memoize.
89 * Supports string, number, boolean, null and undefined arguments. Doesn't
90 * support \x0B characters in the strings.
91 * @param {number} functionUid Unique identifier of the function whose result
92 * is cached.
93 * @param {?{length:number}} args The arguments that the function to memoize is
94 * called with. Note: it is an array-like object, because supports indexing
95 * and has the length property.
96 * @return {string} The list of arguments with type information concatenated
97 * with the functionUid argument, serialized as \x0B-separated string.
98 */
99 goog.memoize.simpleSerializer = function(functionUid, args) {
100 var context = [functionUid];
101 for (var i = args.length - 1; i >= 0; --i) {
102 context.push(typeof args[i], args[i]);
103 }
104 return context.join('\x0B');
105 };