-
-
Save davsket/5493269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////// | |
// 7. Leer del DOM lo menos posible y reusar variables | |
/////////////////////////////////////////////////////// | |
// Realmente maaaal | |
for(var i=0; i < $$('a').length; i++){ | |
if( $$('a')[i].offsetHeight < 100){ // -> <- DOM | |
... | |
// otro código extra | |
... | |
$$('a')[i].style.height = $$('a')[i].offsetHeight + 30 // -> <- DOM | |
... | |
// otro código extra | |
... | |
$$('a')[i].style.color = 'red' // -> <- DOM | |
... | |
// otro código extra | |
... | |
$$('a')[i].style.fontSize = '2rem' // -> <- DOM | |
} | |
} | |
// Un poco mejor | |
var i = 0, | |
as = $$('a'), // oh yeah, mejor | |
len = as.length, // realmente no gran cambio, pero ayuda un tris | |
asStyle | |
for(; i < len; i++){ | |
// -> <- DOM en una pausa | |
if( as[i].offsetHeight < 100){ | |
asStyle = as[i].style | |
asStyle.height = as[i].offsetHeight + 30 | |
asStyle.color = 'red' | |
asStyle.fontSize = '2rem' | |
// -> <- Fin del DOM | |
... | |
// todo el código extra | |
... | |
} | |
} | |
// Aún mejor! | |
var i = 0, | |
as = $$('a'), // oh yeah, mejor | |
len = as.length, // realmente no gran cambio, pero ayuda un tris | |
asStyle, | |
conditionsArray = [] | |
// -> <- DOM en una pausa | |
for(; i < len; i++){ | |
conditionsArray[i] = as[i].offsetHeight < 100 | |
if( conditionsArray[i] ){ | |
asStyle = as[i].style | |
asStyle.height = as[i].offsetHeight + 30 | |
asStyle.color = 'red' | |
asStyle.fontSize = '2rem' | |
} | |
} | |
// -> <- Fin del DOM | |
for(i = 0 ; i < len; i++){ | |
if( conditionsArray[i] ){ | |
... | |
// todo el código extra | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment