Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/277899
Change subject: mobile.browser: Simplify memoize() implementation
......................................................................
mobile.browser: Simplify memoize() implementation
Embrace closure scope instead of fragile binding to 'this' in
a pseudo-private unique property. (Which has overhead, fails in
in detached contexts, and isn't needed.)
Change-Id: I68a673bdfb4087310302a940bf44d4dcdf719a15
---
M resources/mobile.browser/browser.js
1 file changed, 13 insertions(+), 17 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/99/277899/1
diff --git a/resources/mobile.browser/browser.js
b/resources/mobile.browser/browser.js
index 57ebdda..b568793 100644
--- a/resources/mobile.browser/browser.js
+++ b/resources/mobile.browser/browser.js
@@ -2,28 +2,24 @@
var browser;
/**
- * Memoize a class method. Caches the result of the method based on the
- * arguments. Instances do not share a cache.
+ * Memoize a function.
+ *
+ * Cache return values based on the arguments. Instances do not share a
cache.
+ *
* @ignore
- * @param {Function} method Method to be memoized
+ * @param {Function} fun Method to be memoized
* @returns {Function}
*/
- function memoize( method ) {
- /**
- * Memoized version of the method
- * @ignore
- */
- var memoized = function () {
- var cache = this[ '__cache' + memoized.cacheId ] ||
- ( this[ '__cache' + memoized.cacheId ] = {} ),
- key = [].join.call( arguments, '|' );
- if ( cache.hasOwnProperty( key ) ) {
- return cache[ key ];
+ function memoize( fun ) {
+ var cache = {};
+ // Memoized version of the function
+ return function () {
+ var key = [].join.call( arguments, '|' );
+ if ( !cache.hasOwnProperty( key ) ) {
+ cache[ key ] = fun.apply( this, arguments );
}
- return ( cache[ key ] = method.apply( this, arguments )
);
+ return cache[ key ];
};
- memoized.cacheId = Date.now().toString() +
Math.random().toString();
- return memoized;
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/277899
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I68a673bdfb4087310302a940bf44d4dcdf719a15
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits