cyberhobo / ColumnFilterWidgets

This is an add-on for the DataTables plugin for jQuery that creates filtering widgets based on the data in table columns.
69 stars 34 forks source link

Replacing $.each with for loop in ColumnFilterWidget.prototype.fnDraw #38

Open skarv opened 9 years ago

skarv commented 9 years ago

Desperately trying to overcome the issue with 40-50 second page loads in IE11, things improved a bit when replacing a $.each loop with a simple for loop as proposed in this thread: http://datatables.net/forums/discussion/4739/beating-the-ie-dead-horse/p2

I am now having a go at replacing another $.each loop a for loop in the ColumnFilterWidget.prototype.fnDraw function, and the code works well in my early test environment, unless there's something I overlooked?

I replaced:

$.each( aData, function( i, sValue ) { //slow in IE11
    var asValues = widget.sSeparator ? sValue.split( new RegExp( widget.sSeparator ) ) : [ sValue ];
    $.each( asValues, function( j, sOption ) {
        if ( !oDistinctOptions.hasOwnProperty( sOption ) ) {
            oDistinctOptions[sOption] = true;
            aDistinctOptions.push( sOption );
        }
    } );
} );

with:

var aDataLength = aData.length;
for (var i = 0; i < aDataLength; i++) {
    var sValue = aData[i]
    var asValues = widget.sSeparator ? sValue.split( new RegExp( widget.sSeparator ) ) : [ sValue ];   
    for (var a = 0; a < aDataLength; a++) {
        if ( asValues[a] ) {
            var sOption = asValues[a]
            if ( !oDistinctOptions.hasOwnProperty( sOption ) ) { 
                oDistinctOptions[sOption] = true;
                aDistinctOptions.push( sOption );
            }
        }
    }   
}
skarv commented 9 years ago

I notice that you only seem to use the oDistinctOptions object for testing for duplicate values, then pushing the same single value to the aDistinctOptions array for each iteration. Just speculating, could this perhaps be optimized by either copying all data to the array from the object in one go, or either just use the array or the object, depending on which is faster?

I successfully tested out using just the array for checking for duplicate values, which should be faster than using the hasOwnProperty method on the object:

Settings outside of the fnDraw function: var methodIndexOf = params.methodindex || 1 if (!Array.prototype.indexOf) methodIndexOf = 0

Within the fnDraw function:

if ( methodIndexOf == 1 ) { 
    if( aDistinctOptions.indexOf(sOption) === -1 ){ 
        aDistinctOptions.push( sOption )
    }
}
else {
    if ( !oDistinctOptions.hasOwnProperty( sOption ) ) {
        oDistinctOptions[sOption] = true;
        aDistinctOptions.push( sOption );
    }
}