Skip to content

CPU extreamly high when numerous elements are binded. Suggestion included. #9

Open
@sdadds

Description

@sdadds

Works well even though window resize is called when it really has not changed.

I noticed the CPUs running up over 80% with less than 10 elements registered and
very little re-sizing activity (really sluggish performance). The main elements, jQGrids
embedded inside jQuery-UI tabs and accordions within a jQuery-Layout.

Code below helps for my situation.

Thanks.

function loopy() {
// Start the polling loop, asynchronously.
timeout_id = window[ str_setTimeout ](function(){

// Iterate over all elements to which the 'resize' event is bound.
var pixelTolerance = jq_resize[ str_pixelTolerance ];
elems.each(function() {
    //
    //  Modified to reduce the CPU load when there are numerous
    // elements registered.  Tested with five+ re-sizable jQGrids
    // embedded into various tabs and accordion elements all inside
    // of a jQuery layout.
    //  With the default delay of 250 the CPU usage is reduced and
    // re-sizing snappy.
    //
    var isVisible  = false;
    var hasResized = false;
    var elem       = $(this);

    try {
         if ($(elem).is(':visible') ) {
             isVisible = true;
         }
    } catch (eThrow) {
         //alert(eThrow.message);
    }

    // No reason to resize hidden elements
    if (isVisible) {
        try {
            // For pixels round down to nearest integer
            var width  = Math.floor(elem.width());
            var height = Math.floor(elem.height());
            var data   = $.data( this, str_data );
            data.w     = Math.floor(data.w);
            data.h     = Math.floor(data.h);

            // Reduce number of resizes, default 10 pixels
            if (   (Math.abs(width - data.w)  > pixelTolerance)
                || (Math.abs(height - data.h) > pixelTolerance)) {
                hasResized = true;
            }

            //  If element size has changed
            // trigger the 'resize' event and
            // update the data store
            if (hasResized) {
                elem.trigger( str_resize, [ width, height ] );
                //  Some elements will not re-size exactly
                // so try to get the latest size available
                // before storing
                data.w = Math.floor(elem.width());
                data.h = Math.floor(elem.height());
            }
        } catch (eThrow) {
            //alert(eThrow.message);
        }
    }

  });

  // Loop.
  loopy();

}, jq_resize[ str_delay ] );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions