memoize.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @fileoverview Tool for caching the result of expensive deterministic
  16. * functions.
  17. *
  18. * @see http://en.wikipedia.org/wiki/Memoization
  19. *
  20. */
  21. goog.provide('goog.memoize');
  22. /**
  23. * Decorator around functions that caches the inner function's return values.
  24. *
  25. * To cache parameterless functions, see goog.functions.cacheReturnValue.
  26. *
  27. * @param {Function} f The function to wrap. Its return value may only depend
  28. * on its arguments and 'this' context. There may be further restrictions
  29. * on the arguments depending on the capabilities of the serializer used.
  30. * @param {function(number, Object): string=} opt_serializer A function to
  31. * serialize f's arguments. It must have the same signature as
  32. * goog.memoize.simpleSerializer. It defaults to that function.
  33. * @this {Object} The object whose function is being wrapped.
  34. * @return {!Function} The wrapped function.
  35. */
  36. goog.memoize = function(f, opt_serializer) {
  37. var serializer = opt_serializer || goog.memoize.simpleSerializer;
  38. return function() {
  39. if (goog.memoize.ENABLE_MEMOIZE) {
  40. // In the strict mode, when this function is called as a global function,
  41. // the value of 'this' is undefined instead of a global object. See:
  42. // https://developer.mozilla.org/en/JavaScript/Strict_mode
  43. var thisOrGlobal = this || goog.global;
  44. // Maps the serialized list of args to the corresponding return value.
  45. var cache = thisOrGlobal[goog.memoize.CACHE_PROPERTY_] ||
  46. (thisOrGlobal[goog.memoize.CACHE_PROPERTY_] = {});
  47. var key = serializer(goog.getUid(f), arguments);
  48. return cache.hasOwnProperty(key) ?
  49. cache[key] :
  50. (cache[key] = f.apply(this, arguments));
  51. } else {
  52. return f.apply(this, arguments);
  53. }
  54. };
  55. };
  56. /**
  57. * @define {boolean} Flag to disable memoization in unit tests.
  58. */
  59. goog.define('goog.memoize.ENABLE_MEMOIZE', true);
  60. /**
  61. * Clears the memoization cache on the given object.
  62. * @param {Object} cacheOwner The owner of the cache. This is the {@code this}
  63. * context of the memoized function.
  64. */
  65. goog.memoize.clearCache = function(cacheOwner) {
  66. cacheOwner[goog.memoize.CACHE_PROPERTY_] = {};
  67. };
  68. /**
  69. * Name of the property used by goog.memoize as cache.
  70. * @type {string}
  71. * @private
  72. */
  73. goog.memoize.CACHE_PROPERTY_ = 'closure_memoize_cache_';
  74. /**
  75. * Simple and fast argument serializer function for goog.memoize.
  76. * Supports string, number, boolean, null and undefined arguments. Doesn't
  77. * support \x0B characters in the strings.
  78. * @param {number} functionUid Unique identifier of the function whose result
  79. * is cached.
  80. * @param {?{length:number}} args The arguments that the function to memoize is
  81. * called with. Note: it is an array-like object, because supports indexing
  82. * and has the length property.
  83. * @return {string} The list of arguments with type information concatenated
  84. * with the functionUid argument, serialized as \x0B-separated string.
  85. */
  86. goog.memoize.simpleSerializer = function(functionUid, args) {
  87. var context = [functionUid];
  88. for (var i = args.length - 1; i >= 0; --i) {
  89. context.push(typeof args[i], args[i]);
  90. }
  91. return context.join('\x0B');
  92. };