From ad0e08585f46a33be304e0430636963f4d530890 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Sat, 6 Apr 2013 09:36:44 +0100 Subject: [PATCH] Performance: Width of scrollbars isn't going to change on a single page between table's being reinitialised, so calculating the scrollbar width every time is a real hit on performance since it needs to manipulate the DOM. This change ensures that the calculation is performed only once. --- media/src/core/core.sizing.js | 63 +++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/media/src/core/core.sizing.js b/media/src/core/core.sizing.js index ceb1571b..d5f5feb4 100644 --- a/media/src/core/core.sizing.js +++ b/media/src/core/core.sizing.js @@ -378,36 +378,41 @@ function _fnStringToCss( s ) */ function _fnScrollBarWidth () { - var inner = $('

').css( { - width: '100%', - height: 200, - padding: 0 - } )[0]; - - var outer = $('

') - .css( { - position: 'absolute', - top: 0, - left: 0, - width: 200, - height: 150, - padding: 0, - overflow: 'hidden', - visibility: 'hidden' - } ) - .append( inner ) - .appendTo( 'body' ); - - var w1 = inner.offsetWidth; - outer.css( 'overflow', 'scroll' ); - var w2 = inner.offsetWidth; - if ( w1 === w2 ) + if ( ! DataTable.__scrollbarWidth ) { - w2 = outer[0].clientWidth; - } - - outer.remove(); + var inner = $('

').css( { + width: '100%', + height: 200, + padding: 0 + } )[0]; - return w1 - w2; + var outer = $('

') + .css( { + position: 'absolute', + top: 0, + left: 0, + width: 200, + height: 150, + padding: 0, + overflow: 'hidden', + visibility: 'hidden' + } ) + .append( inner ) + .appendTo( 'body' ); + + var w1 = inner.offsetWidth; + outer.css( 'overflow', 'scroll' ); + var w2 = inner.offsetWidth; + if ( w1 === w2 ) + { + w2 = outer[0].clientWidth; + } + + outer.remove(); + + DataTable.__scrollbarWidth = w1 - w2; + } + + return DataTable.__scrollbarWidth; }