Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery plug-in data access -- performance hit?
    primarykey
    data
    text
    <p>I'm writing a new jQuery plugin. For the guide, I am using their recommendation:</p> <pre><code>(function( $ ){ var methods = { init : function( options ) { return this.each(function(){ var $this = $(this), data = $this.data('tooltip'), tooltip = $('&lt;div /&gt;', { text : $this.attr('title') }); // If the plugin hasn't been initialized yet if ( ! data ) { data = { element : this, target : $this, tooltip : tooltip }; $(this).data('tooltip', data); } methods.update.apply(data.element, 'Test'); }, update : function( content ) { var $this = $(this), data = $this.data('tooltip'); // check or change something important in the data. private.test.apply( data.element ); return data.element; } }; var private = { test: function() { var $this = $(this), data = $this.data('tooltip'); // again, do some operation with data } }; $.fn.tooltip = function( method ) { // Method calling logic 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 on jQuery.tooltip' ); } }; })( jQuery ); </code></pre> <p>Its a little different from their version to make it shorter but also to show my differences. Basically, in the init, I am instantiating and creating data object that gets stored in the element. Part of the data object is the element itself:</p> <pre><code>element : this, </code></pre> <p>Then, after all of the initialization is done, I call a public method from the init (lets say I do it for functionality reuse purpose). To make the call, I use .apply() and provide the proper context (my element), which would match the context when the function is called externally:</p> <pre><code>return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); </code></pre> <p>This is fine and understandable. However, what I am unsure about is the performance of acquiring the data of the plugin from within a private or a public method. To me, it seems that at the top of every public and private method I have to execute the following lines in order to get the data:</p> <pre><code>var $this = $(this), data = $this.data('tooltip'); </code></pre> <p>Of course, I wouldn't execute them when I have no need for whatever is stored in data. However, my plugin performs quite a bit of animations and state tracking and almost all of the functions require access to the data. As such, it seems like accessing .data() in almost every private and public call is a pretty big performance hit.</p> <p>My question is whether anyone uses this plug-in structure (I'm hoping that yes since jQuery recommends it) and has found a different way of referencing the data without hitting .data() in every function call.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload