Path: blob/trunk/third_party/closure/goog/memoize/memoize.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Tool for caching the result of expensive deterministic16* functions.17*18* @see http://en.wikipedia.org/wiki/Memoization19*20*/2122goog.provide('goog.memoize');232425/**26* Decorator around functions that caches the inner function's return values.27*28* To cache parameterless functions, see goog.functions.cacheReturnValue.29*30* @param {Function} f The function to wrap. Its return value may only depend31* on its arguments and 'this' context. There may be further restrictions32* on the arguments depending on the capabilities of the serializer used.33* @param {function(number, Object): string=} opt_serializer A function to34* serialize f's arguments. It must have the same signature as35* goog.memoize.simpleSerializer. It defaults to that function.36* @this {Object} The object whose function is being wrapped.37* @return {!Function} The wrapped function.38*/39goog.memoize = function(f, opt_serializer) {40var serializer = opt_serializer || goog.memoize.simpleSerializer;4142return function() {43if (goog.memoize.ENABLE_MEMOIZE) {44// In the strict mode, when this function is called as a global function,45// the value of 'this' is undefined instead of a global object. See:46// https://developer.mozilla.org/en/JavaScript/Strict_mode47var thisOrGlobal = this || goog.global;48// Maps the serialized list of args to the corresponding return value.49var cache = thisOrGlobal[goog.memoize.CACHE_PROPERTY_] ||50(thisOrGlobal[goog.memoize.CACHE_PROPERTY_] = {});51var key = serializer(goog.getUid(f), arguments);52return cache.hasOwnProperty(key) ?53cache[key] :54(cache[key] = f.apply(this, arguments));55} else {56return f.apply(this, arguments);57}58};59};606162/**63* @define {boolean} Flag to disable memoization in unit tests.64*/65goog.define('goog.memoize.ENABLE_MEMOIZE', true);666768/**69* Clears the memoization cache on the given object.70* @param {Object} cacheOwner The owner of the cache. This is the {@code this}71* context of the memoized function.72*/73goog.memoize.clearCache = function(cacheOwner) {74cacheOwner[goog.memoize.CACHE_PROPERTY_] = {};75};767778/**79* Name of the property used by goog.memoize as cache.80* @type {string}81* @private82*/83goog.memoize.CACHE_PROPERTY_ = 'closure_memoize_cache_';848586/**87* Simple and fast argument serializer function for goog.memoize.88* Supports string, number, boolean, null and undefined arguments. Doesn't89* support \x0B characters in the strings.90* @param {number} functionUid Unique identifier of the function whose result91* is cached.92* @param {?{length:number}} args The arguments that the function to memoize is93* called with. Note: it is an array-like object, because supports indexing94* and has the length property.95* @return {string} The list of arguments with type information concatenated96* with the functionUid argument, serialized as \x0B-separated string.97*/98goog.memoize.simpleSerializer = function(functionUid, args) {99var context = [functionUid];100for (var i = args.length - 1; i >= 0; --i) {101context.push(typeof args[i], args[i]);102}103return context.join('\x0B');104};105106107