From 2c14233057d689cd9fb1b6bda028682103dcc290 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Sat, 19 Nov 2011 09:53:11 +0000 Subject: [PATCH] New: Optimisation for sorting - when the sorting runs it will execute the sorting function a lot, particularly for large tables, so we want these functions to be as fast as possible. As it stood, each time the function ran it would prep the data and then do the required comparison - and it would do that data prep every time. This is pointless since toLowerCase (for example) is always going to give the same result when using the same input - so now introduced a third sorting function type (in addition to 'asc' and 'desc') called 'pre', which will pre-format the data to be sorted, only once for each item to be sorted, allowing the sorting comparison function itself to be really fast (simply just the comparison). This is also backwards compatible, if the sorting type doesn't have a 'pre'-formatting method then it will just use the two sorting functions as normal. --- media/js/jquery.dataTables.js | 104 +++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index fd0cc201..e3b033c3 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -581,21 +581,19 @@ /* * text sorting */ - "string-asc": function ( a, b ) + "string-pre": function ( a ) { if ( typeof a != 'string' ) { a = ''; } - if ( typeof b != 'string' ) { b = ''; } - var x = a.toLowerCase(); - var y = b.toLowerCase(); + return a.toLowerCase(); + }, + + "string-asc": function ( x, y ) + { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }, - "string-desc": function ( a, b ) + "string-desc": function ( x, y ) { - if ( typeof a != 'string' ) { a = ''; } - if ( typeof b != 'string' ) { b = ''; } - var x = a.toLowerCase(); - var y = b.toLowerCase(); return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }, @@ -603,17 +601,18 @@ /* * html sorting (ignore html tags) */ - "html-asc": function ( a, b ) + "html-pre": function ( a ) + { + return a.replace( /<.*?>/g, "" ).toLowerCase(); + }, + + "html-asc": function ( x, y ) { - var x = a.replace( /<.*?>/g, "" ).toLowerCase(); - var y = b.replace( /<.*?>/g, "" ).toLowerCase(); return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }, - "html-desc": function ( a, b ) + "html-desc": function ( x, y ) { - var x = a.replace( /<.*?>/g, "" ).toLowerCase(); - var y = b.replace( /<.*?>/g, "" ).toLowerCase(); return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }, @@ -621,37 +620,24 @@ /* * date sorting */ - "date-asc": function ( a, b ) + "date-pre": function ( a ) { var x = Date.parse( a ); - var y = Date.parse( b ); if ( isNaN(x) || x==="" ) { - x = Date.parse( "01/01/1970 00:00:00" ); + x = Date.parse( "01/01/1970 00:00:00" ); } - if ( isNaN(y) || y==="" ) - { - y = Date.parse( "01/01/1970 00:00:00" ); - } - + return x; + }, + + "date-asc": function ( x, y ) + { return x - y; }, - "date-desc": function ( a, b ) + "date-desc": function ( x, y ) { - var x = Date.parse( a ); - var y = Date.parse( b ); - - if ( isNaN(x) || x==="" ) - { - x = Date.parse( "01/01/1970 00:00:00" ); - } - if ( isNaN(y) || y==="" ) - { - y = Date.parse( "01/01/1970 00:00:00" ); - } - return y - x; }, @@ -659,17 +645,18 @@ /* * numerical sorting */ - "numeric-asc": function ( a, b ) + "numeric-asc": function ( a ) + { + return (a=="-" || a==="") ? 0 : a*1; + }, + + "numeric-asc": function ( x, y ) { - var x = (a=="-" || a==="") ? 0 : a*1; - var y = (b=="-" || b==="") ? 0 : b*1; return x - y; }, - "numeric-desc": function ( a, b ) + "numeric-desc": function ( x, y ) { - var x = (a=="-" || a==="") ? 0 : a*1; - var y = (b=="-" || b==="") ? 0 : b*1; return y - x; } }; @@ -2688,6 +2675,7 @@ "nTr": null, "_iId": oSettings.iNextId++, "_aData": aDataIn, + "_aSortData": [], "_anHidden": [], "_sRowStripe": "" }; @@ -2843,6 +2831,7 @@ "nTr": nTrs[i], "_iId": oSettings.iNextId++, "_aData": [], + "_aSortData": [], "_anHidden": [], "_sRowStripe": '' } ); @@ -4649,7 +4638,7 @@ function _fnSort ( oSettings, bApplyClasses ) { var - i, iLen, j, jLen, + i, iLen, j, jLen, k, kLen, aaSort = [], aiOrig = [], oSort = _oExt.oSort, @@ -4694,6 +4683,30 @@ { aiOrig[ oSettings.aiDisplayMaster[i] ] = i; } + + /* Build an internal data array which is specific to the sort, so we can get and prep + * the data to be sorted only once, rather than needing to do it every time the sorting + * function runs. This make the sorting function a very simple comparison + */ + var iSortLen = aaSort.length; + var fnSortFormat; + for ( i=0, iLen=aoData.length ; i