// run this first // this function adds some divs at the end of body (embeded into a CENTER tag) // this variant is quite fast (it can get slower only when ADDING new innerHTML // into element that already has a HUGE one [whats not this case]) // you can run and/or tweak it repeatedly // (beware, more divs = more loops) var how_many_divs = 1000; var container = document.createElement('center'); container.innerHTML = new Array(how_many_divs+1).join('<div>Hej</div>'); document.body.appendChild(container); window.items = document.getElementsByTagName('div'); //alert(window.items.length); // this function does the same as m_init // this variant is quite slow, // but cannot be slowed much more. var how_many_divs = 1000; var container = document.createElement('center'); for (var i=0, elm; i<how_many_divs; i++) { elm = document.createElement("div"); elm.innerHTML = "Hej"; container.appendChild(elm); // Robert, you really should read PPKs work (again) : ) // http://quirksmode.org/dom/innerhtml.html } document.body.appendChild(container); window.items = document.getElementsByTagName('div'); // do not run this alone, it is meant to be called from within loops // div item reference is passed as a first argument // erase or change its content and re-'Make' it if you like. /* * / if ( arguments[0] ) { arguments[0].className = arguments[0].className == 'foo' ? 'bar' : 'foo'; } else { document.title = 'Duh!'; } /* */ var items = window.items, item; for (var i=0; i < items.length; i++) { item = items[i]; window.functions.magic(item); } var items = window.items, item; for (var i=0; items[i]; i++) { item = items[i]; window.functions.magic(item); } var items = window.items, item; for (var i=0, il=items.length; i<il; i++) { item = items[i]; window.functions.magic(item); } var items = window.items, item; var i = 0, il = items.length; do { item = items[i++]; window.functions.magic(item); } while ( i < il ); // (below functions are...) // just for EXISTING NOT-EMPTY CONTINUOUS LISTS // 'existing' = defined : ) // 'not-empty' = containing at least one not 'null item' // 'continuous' = not containing any 'null' item // 'list' = one dimensional array or nodeList // 'null' = anything that converts to false (undefined,0,aso.) var items = window.items, item; var i = 0, item = items[i]; do { // item is already defined window.functions.magic(item); } while ( item = items[++i] ); var items = window.items, item; var i = -1; while ( item = items[++i]) { // item is already defined window.functions.magic(item); } var items = window.items, item; var i = 0; while ( item = items[i++]) { // item is already defined window.functions.magic(item); } // more a parody, but it works : ) var items = window.items, item; var arr = new Array(window.items.length+1); if ( !window.fn ) { window.fn = new Function( 'var i = -1;' + arr.join( 'window.functions.magic(window.items[++i]);' ) ); } window.fn(); var items = window.items, item; var i = items.length; while ( i-- ) { item = items[i]; window.functions.magic(item); } var items = window.items, item; var i = items.length+1; while ( i ) { item = items[--i]; window.functions.magic(item); }

http://www.robertnyman.com/2008/04/11/javascript-loop-performance/