Showing posts with label best practice. Show all posts
Showing posts with label best practice. Show all posts

Friday, January 25, 2013

A great collection of App performance articles


This is a blog post quote from other people, but want to put it here as self reference in the future and in the hope that it will help more people. 

There have been some great app performance resources surfacing recently, so I thought it would make sense to do a roundup.

I've recently seen apps whose performance is being crushed by large & expensive paints, extra layouts, bad event decoupling or too many network requests. They are all fixable items, and Chrome's DevTools and tracing tool both do a great job of showing you what's going on under the hood.

Web Performance Power Tool: HTTP Archive (HAR)
A rundown of the HAR file and how you can use it to diagnose the network side of your application, with +Ilya Grigorik and +Peter Lubbers.

Wait, DevTools could do THAT?
A fantastic presentation by +Ilya Grigorik on Chrome's DevTools

Progressive jpegs: a new best practice
Ann Robson explains how we can all reduce our apps' bandwidth consumption by making use of progressive jpegs

Structural and Sampling (JavaScript) Profiling in Google Chrome
+John McCutchan and +Ilya Grigorik (he keeps showing up this guy!) cover Chrome's tracing tool. If you're a power user, this one is a must.

Snow in canvas land
+Jake Archibald relives the horror of his 2-year-old code, and sets about fixing it

Improving Web App Performance With the Chrome DevTools Timeline and Profiles
A very thorough tutorial (with ANIMATED GIFS!) by +Addy Osmani on how to use Chrome DevTools to diagnose memory and performance issues.

Scrolling Performance and Parallaxin'
Two articles by yours truly on understanding and improving scrolling performance in your applications

Google I/O 2012 - Jank Busters: Building Performant Web Apps
+Nat Duca and +Tom Wiltzius of Google's Chrome GPU team cover a host of tips & tricks for maximizing your app's rendering performance

Why moving elements with translate() is better than pos:abs top/left
+Paul Irish on using translates vs absolute positioning, and the performance implications of both

Thursday, October 13, 2011

jQuery Plugin Patterns

Here are some jQuery Plugin Patterns, the list is still expanding.  This post is mostly for my own reference, but just blog it here in case someone might be helped from this.

One note before you start doing your super fancy plugin.  The 'this' keyword in the immediate scope of the plugin is referring to the jQuery object, so there is no need to do $(this).  


Basic Form


  $.fn.yourPlugin = function(options, callback) {
    // ...
  };



Wrapped in Closure to create namespace

;(function($, window, undefined) {
  $.fn.yourPlugin = function(options, callback) {
    // ...
  };
})(jQuery, window);



Define multiple related functions (or related plugins) in one shot. 

;(function($, window, undefined) {
  $.extend($.fn, {
     yourPlugin: function() {
        // ...
     },
     yourSecondPlugin: function() {
        // ...
     }
  });
})(jQuery, window);


Prototype way

;(function($, window, undefined) {
  var pluginName = 'yourPlugin', pluginDefaultOptions = {};

  function Plugin(el, options) {
    this.element = el;
    this.options = $.extend({}, pluginDefaultOptions, options);
    this._name = pluginName;
    this.init();
  }  

  Plugin.prototype.init = function() {
    // ...
  }

  // make sure only one instance is instantiated
  $.fn[pluginName] = function ( options ) {
    return this.each(function () {
       if (!$.data(this, 'plugin_' + pluginName)) {
           $.data(this, 'plugin_' + pluginName,
           new Plugin( this, options ));
       }
    });
  }

})(jQuery, window);


Object Literal way

;(function($, window, undefined) {

    $.fn.pluginName = function(method) {

        var methods = {
            init : function(options) {
                this.pluginName.settings = $.extend({}, 
                                             this.pluginName.defaults, 
                                             options);
                return this.each(function() {
                    var $element = $(this), 
                         element = this;  
                    // ...
                });
            },

            my_public_method: function() {
                // ...
            }
        }

        var helpers = {
            my_private_method: function() {
                // ...
            }
        }

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error( 'Method "' +  method + '" does not exist in pluginName plugin!');
        }
    }

    $.fn.pluginName.defaults = {
        foo: 'bar'
    }

    $.fn.pluginName.settings = {}

})(jQuery, window);



Modified Object Literal way

;(function($, window, undefined){
    $.fn.packagename = function(options, callback){
        
        var params = $.extend({},
                        $.fn.packagename.default_options, options), 
                        $that = $(this);
        
        if($.isFunction(options)){
            callback = options;
            options = {};
        } 
        
        var methods = {
            init: function() {
               // ... 
            }
        }
        
        methods.init.apply(this,[]);      
    }
    
    $.fn.packagename.default_options = {
       src: ".data" 
    }
})(jQuery, window);