Skip to content

Instantly share code, notes, and snippets.

@yavor-atanasov
Created April 27, 2011 11:05
Show Gist options
  • Save yavor-atanasov/944065 to your computer and use it in GitHub Desktop.
Save yavor-atanasov/944065 to your computer and use it in GitHub Desktop.
CSSP look-up optimization
/*
Previous proposal (focus on the code within the hash blocks):
*/
function look() {
var cssp,
elem;
if (!document.body) {
return;
}
for (var i = 0; i < csspQueue.length; i++) { // length may change during loop
cssp = csspQueue[i];
/* ################################################################################# */
if(typeof cssp[0] === 'string'){
elem = getElement(cssp[0]);
// is there a test element to look for?
if (!elem) {
elem = addTestElement(cssp[0]);
}
cssp[0] = elem;
}
else{
elem = cssp[0];
}
/* ################################################################################# */
if ( getZindex(elem) === magicNumber ) {
cssp[1](); // fire callback
csspQueue.splice(i--, 1); // remove this item from the queue, decrement i
if (csspQueue.length === 0) { // can stop looking now
clearInterval(intervalId);
}
}
}
}
/*
New proposal (focus on the code within the hash blocks):
Instead of doing odd shenanigans with the cssp[0] item, it would be much cleaner and read better
to check if the elem is undefined and if it is, assign an element to it. Tests pass.
*/
function look() {
var cssp,
elem;
if (!document.body) {
return;
}
for (var i = 0; i < csspQueue.length; i++) { // length may change during loop
cssp = csspQueue[i];
/* ################################################################################# */
if(typeof elem === 'undefined'){
elem = getElement(cssp[0]);
// is there a test element to look for?
if (!elem) {
elem = addTestElement(cssp[0]);
}
}
/* ################################################################################# */
if ( getZindex(elem) === magicNumber ) {
cssp[1](); // fire callback
csspQueue.splice(i--, 1); // remove this item from the queue, decrement i
if (csspQueue.length === 0) { // can stop looking now
clearInterval(intervalId);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment