Jhernandez has uploaded a new change for review. https://gerrit.wikimedia.org/r/181394
Change subject: Memoize results of the browser.js methods. ...................................................................... Memoize results of the browser.js methods. Some browser detection methods are a bit involved (they create dom nodes and do several things) to be calling them several times on page load. By caching the results they will be calculated just once. The isWideScreen method is not cached because the width of the browser may change in desktop versions. Change-Id: I0b8a863095fdcc84956b3e711baecf5936b99506 --- M javascripts/browser.js 1 file changed, 41 insertions(+), 16 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/94/181394/1 diff --git a/javascripts/browser.js b/javascripts/browser.js index 04abfd1..e67f810 100644 --- a/javascripts/browser.js +++ b/javascripts/browser.js @@ -2,6 +2,31 @@ var browser; /** + * Memoize a class method. Caches the result of the method based on the + * arguments. Instances do not share a cache. + * @ignore + * @param {Function} method 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 ]; + } + return ( cache[ key ] = method.apply( this, arguments ) ); + }; + memoized.cacheId = '' + Date.now() + Math.random(); + return memoized; + } + + /** * Representation of user's current browser * @class Browser * @param {String} ua the user agent of the current browser @@ -21,9 +46,9 @@ * FIXME: jquery.client does not support iPad detection so we cannot use it. * @return {Boolean} */ - isIos: function () { + isIos: memoize( function () { return /ipad|iphone|ipod/i.test( this.userAgent ); - }, + } ), /** * Locks the viewport so that pinch zooming is disabled */ @@ -38,9 +63,9 @@ * @method * @return {Boolean} */ - isAndroid2: function () { + isAndroid2: memoize( function () { return /Android 2/.test( this.userAgent ); - }, + } ), /** * Determine if a device has a widescreen. * @method @@ -60,7 +85,7 @@ * * @returns {boolean} */ - supportsAnimations: function () { + supportsAnimations: memoize( function () { var has3d, t, el = $( '<p>' )[0], $iframe = $( '<iframe>' ), @@ -90,7 +115,7 @@ $iframe.remove(); return has3d !== undefined && has3d.length > 0 && has3d !== 'none'; - }, + } ), /** * Detect if fixed position is supported in browser * http://www.quirksmode.org/blog/archives/2010/12/the_fifth_posit.html @@ -99,7 +124,7 @@ * @method * @return {Boolean} */ - supportsPositionFixed: function () { + supportsPositionFixed: memoize( function () { var support = false, userAgent = this.userAgent; @@ -118,30 +143,30 @@ } } ); return support; - }, + } ), /** * Whether touchstart and other touch events are supported by the current browser. * * @method * @return {Boolean} */ - supportsTouchEvents: function () { + supportsTouchEvents: memoize( function () { return 'ontouchstart' in window; - }, + } ), /** * Detect if browser supports geolocation * @method * @return {Boolean} */ - supportsGeoLocation: function () { + supportsGeoLocation: memoize( function () { return !!navigator.geolocation; - }, + } ), /** * Detect if local storage * @method * @return {Boolean} */ - supportsLocalStorage: function () { + supportsLocalStorage: memoize( function () { // See if local storage is supported try { localStorage.setItem( 'localStorageTest', 'localStorageTest' ); @@ -150,12 +175,12 @@ } catch ( e ) { return false; } - }, + } ), /** * Detect if we support file input uploads * @return {Boolean} */ - supportsFileUploads: function () { + supportsFileUploads: memoize( function () { var browserSupported; // If already calculated, just return it if ( this._fileUploads !== undefined ) { @@ -176,7 +201,7 @@ !mw.config.get( 'wgImagesDisabled', false ); } return this._fileUploads; - } + } ) }; browser = new Browser( window.navigator.userAgent, $( 'html' ) ); -- To view, visit https://gerrit.wikimedia.org/r/181394 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0b8a863095fdcc84956b3e711baecf5936b99506 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jhernandez <jhernan...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits