I am jQuery noob trying to track the performance issue that we have with large tables. We have a home table widget that, by the way, sets the column width to the maximum header width or row width.
With a table of 10 rows per 500 columns, this can take almost 40 seconds (!), Which seems excessive given that the table can be displayed in less than a second.
Here's the whole function:
var dataTable = this._div.find('.data-table'); var headerTable = this._div.find('.data-header-table'); if (dataTable.length === 0 || headerTable.length === 0) { return; } dataTable = dataTable[0]; headerTable = headerTable[0]; if (dataTable.rows.length === 0) { return; } var dataTr = dataTable.rows[0]; var headerTr = headerTable.rows[headerTable.rows.length - 1]; var marginColumnWidths = $(dataTr.cells[0]).outerWidth(true) + $(dataTr.cells[1]).outerWidth(true) + $(dataTr.cells[2]).outerWidth(true) - DOM.getHPaddings($(headerTr.cells[0])) + 1; $(headerTable) .find('> tbody > tr > td:first-child') .width('1%') .children() .first() .width(marginColumnWidths); for (var i = 1; i < headerTr.cells.length; i++) { var headerTd = $(headerTr.cells[i]); var dataTd = $(dataTr.cells[i + 2]); var commonWidth = Math.max( Math.min(headerTd.width(), 100), dataTd.width() ); headerTd.width('1%').find('> div').width(commonWidth); dataTd.children().first().width(commonWidth); }
If I replaced
var commonWidth = Math.max( Math.min(headerTd.width(), 100), dataTd.width() );
with constant
var commonWidth = 100;
the execution time is reduced from 38 seconds to a second, which indicates that the problem is with reading / calculating the current width, and not with setting a new width. From the profiling / fetching I've done, it seems like jQuery is spending an excessive amount of time using CSS computations.
Can a more efficient way to do this be recommended?
EDIT:
I tried both css ("width") and outerWidth () without any significant performance changes. We used jQuery 1.7.2, but updating to 1.8.1 did not affect performance much.
performance jquery
Tom morris
source share