Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved performance by only modifying the DOM when styles have changed.

  • Loading branch information...
commit 9dd5173df222892c3987ddc0e72aeb91a9ee3752 1 parent 64823e7
Kyle Robertson authored
Showing with 15 additions and 2 deletions.
  1. +15 −2 respond.src.js
View
17 respond.src.js
@@ -17,6 +17,7 @@
docElem = doc.documentElement,
mediastyles = [],
rules = [],
+ currentIds = "",
appendedEls = [],
parsedSheets = {},
resizeThrottle = 30,
@@ -116,6 +117,7 @@
for( ; j < eql; j++ ){
thisq = eachq[ j ];
mediastyles.push( {
+ id : i + j,
media : thisq.match( /(only\s+)?([a-zA-Z]+)(\sand)?/ ) && RegExp.$2,
rules : rules.length - 1,
minw : thisq.match( /\(min\-width:[\s]*([\s]*[0-9]+)px[\s]*\)/ ) && parseFloat( RegExp.$1 ),
@@ -138,7 +140,8 @@
currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp,
styleBlocks = {},
lastLink = links[ links.length-1 ],
- now = (new Date()).getTime();
+ now = (new Date()).getTime(),
+ newIds = [];
//throttle resize calls
if( fromResize && lastCall && now - lastCall < resizeThrottle ){
@@ -150,7 +153,7 @@
lastCall = now;
}
- for( var i in mediastyles ){
+ for( var i = 0; i < mediastyles.length; i++ ){
var thisstyle = mediastyles[ i ];
if( !thisstyle.minw && !thisstyle.maxw ||
( !thisstyle.minw || thisstyle.minw && currWidth >= thisstyle.minw ) &&
@@ -158,10 +161,20 @@
if( !styleBlocks[ thisstyle.media ] ){
styleBlocks[ thisstyle.media ] = [];
}
+ newIds.push( thisstyle.id );
styleBlocks[ thisstyle.media ].push( rules[ thisstyle.rules ] );
}
}
+ //skip if nothing has changed
+ newIds = newIds.join("-");
+ if( newIds === currentIds ) {
+ return;
+ }
+ else {
+ currentIds = newIds;
+ }
+
//remove any existing respond style element(s)
for( var i in appendedEls ){
if( appendedEls[ i ] && appendedEls[ i ].parentNode === head ){
Please sign in to comment.
Something went wrong with that request. Please try again.