Skip to content

Instantly share code, notes, and snippets.

@davsket
Created May 1, 2013 01:49
Show Gist options
  • Save davsket/5493269 to your computer and use it in GitHub Desktop.
Save davsket/5493269 to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////
// 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