Hi I am facing problem with following JavaScript ,I am generating a tree, if the records are 300 , its working fine ,If the records are 1000 , its taking to much time(2 min) for generating tree ,Pls suggest me to increase the performance The following script i am using
// Arrays for nodes and iconsvar nodes = new Array();;var openNodes = new Array();var icons = new Array(6);// Loads all icons that are used in the treefunction preloadIcons(prefix) { icons[0] = new Image(); icons[0].src = prefix+"/plus.gif"; icons[1] = new Image(); icons[1].src = prefix+"/plusbottom.gif"; icons[2] = new Image(); icons[2].src = prefix+"/minus.gif"; icons[3] = new Image(); icons[3].src = prefix+"/minusbottom.gif";}// Create the treefunction createTree(arrName, startNode, openNode, prefix, prefixHostURL) { nodes = arrName; if (nodes.length > 0) { preloadIcons(prefix); var recursedNodes = new Array(); var d1 = new Date(); //document.write("D:"+d+"<br>"); document.write("Start Time ....."+d1.getHours()+":"+d1.getMinutes()+":"+d1.getSeconds()+":"+d1.getMilliseconds()+"<br>"); addNode(startNode, recursedNodes, prefix, prefixHostURL); var d2 = new Date(); document.write("<br>End Time ......."+d2.getHours()+":"+d2.getMinutes()+":"+d2.getSeconds()+":"+d2.getMilliseconds()); document.write("<br>Total Time diff. ......."+(d2.getMinutes()-d1.getMinutes())+":"+(d2.getSeconds()-d1.getSeconds())+":"+(d2.getMilliseconds()-d1.getMilliseconds())); }}// Checks if a node is openfunction isNodeOpen(node) { for (i=0; i<openNodes.length; i++) if (openNodes[i]==node) return true; return false;}// Checks if a node has any childrenfunction hasChildNode(parentNode) { for (i=0; i< nodes.length; i++) { var nodeValues = nodes[i].split("|"); if (nodeValues[1] == parentNode) return true; } return false;}// Checks if a node is the last siblingfunction lastSibling (node, parentNode) { var lastChild = 0; for (i=0; i< nodes.length; i++) { var nodeValues = nodes[i].split("|"); if (nodeValues[1] == parentNode) lastChild = nodeValues[0]; } if (lastChild==node) { return true; } return false;}// Adds a new node to the treefunction addNode(parentNode, recursedNodes, prefix, prefixHostURL) { for (var i = 0; i < nodes.length; i++) { var nodeValues = nodes[i].split("|"); if (nodeValues[1] == parentNode) { var ls = lastSibling(nodeValues[0], nodeValues[1]); var hcn = hasChildNode(nodeValues[0]); var ino = isNodeOpen(nodeValues[0]); // Write out line & empty icons for (g=0; g<recursedNodes.length; g++) { if (recursedNodes[g] == 1) document.write("<img src='"+prefix+"/line.gif' align=\"absbottom\" alt=\"\" />"); else document.write("<img src='"+prefix+"/empty.gif' align=\"absbottom\" alt=\"\" />"); } // put in array line & empty icons if (ls) recursedNodes.push(0); else recursedNodes.push(1); // Write out join icons if (hcn) { if (ls) { document.write("<a href=\"java script: oc(" + nodeValues[0] + ", 1);\"><img border=\"0\" id=\"join" + nodeValues[0] + "\" src='"+prefix+"/"); if (ino) document.write("minus"); else document.write("plus"); document.write("bottom.gif' align=\"absbottom\" alt=\"Open/Close node\" /></a>"+ "<img src='"+prefix+"/space.gif' align=\"absbottom\" alt=\"\" />"); } else { document.write("<a href=\"java script: oc(" + nodeValues[0] + ", 0);\"><img border=\"0\" id=\"join" + nodeValues[0] + "\" src='"+prefix+"/"); if (ino) document.write("minus"); else document.write("plus"); document.write(".gif' align=\"absbottom\" alt=\"Open/Close node\" /></a>"+ "<img src='"+prefix+"/space.gif' align=\"absbottom\" alt=\"\" />"); } } else { if (ls) document.write("<img src='"+prefix+"/joinbottom.gif' align=\"absbottom\" alt=\"\" />"+ "<img src='"+prefix+"/space.gif' align=\"absbottom\" alt=\"\" />"); else document.write("<img src='"+prefix+"/join.gif' align=\"absbottom\" alt=\"\" />"+ "<img src='"+prefix+"/space.gif' align=\"absbottom\" alt=\"\" />"); } // Write out node name var index = 0; var treeval = nodes[i]; //for getting node id index = treeval.indexOf("|"); var ndid = treeval.substring(0,index); treeval = treeval.substring(index+1); //for getting parent node id index = treeval.indexOf("|"); var pndid = treeval.substring(0,index); treeval = treeval.substring(index+1); //for getting platform/product id index = treeval.indexOf("|"); var id = treeval.substring(0,index); treeval = treeval.substring(index+1); if(pndid == "0") { var val = ndid + "|" + id; index = 0; var ctype = "";//myPlatform; while((index = ctype.indexOf(",")) > -1) { var val = ctype.substring(0,index); if(val == id) { break; } ctype = ctype.substring(index+1); } } else { var val = pndid + "|" + id; index = 0; var ctype = "";//myProduct; while((index = ctype.indexOf(",")) > -1) { var val = ctype.substring(0,index); if(val == id) { break; } ctype = ctype.substring(index+1); } } if(nodeValues[1] == "0") { document.write("<b>"+"<a href='"+prefixHostURL+nodeValues[3]+"' target=\"_blank\">"+nodeValues[2]+"</a>"+"</b>"); } else { document.write("<a href='"+prefixHostURL+nodeValues[3]+"' target=\"_blank\">"+nodeValues[2]+"</a>"); } // End link document.write("<br />"); // If node has children write out divs and go deeper if (hcn) { document.write("<div id=\"div" + nodeValues[0] + "\""); if (!ino) document.write(" style=\"display: none;\""); document.write(">"); addNode(nodeValues[0], recursedNodes, prefix, prefixHostURL); document.write("</div>"); } // remove last line or empty icon recursedNodes.pop(); } }}// Opens or closes a nodefunction oc(node, bottom) { var theDiv = document.getElementById("div" + node); var theJoin = document.getElementById("join" + node); var theIcon = document.getElementById("icon" + node); if (theDiv.style.display == 'none') { if (bottom==1) theJoin.src = icons[3].src; else theJoin.src = icons[2].src; theDiv.style.display = ''; } else { if (bottom==1) theJoin.src = icons[1].src; else theJoin.src = icons[0].src; theDiv.style.display = 'none'; }}// Push and pop not implemented in IEif(!Array.prototype.push) { function array_push() { for(var i=0;i<arguments.length;i++) this[this.length]=arguments[i]; return this.length; } Array.prototype.push = array_push;}if(!Array.prototype.pop) { function array_pop(){ lastElement = this[this.length-1]; this.length = Math.max(this.length-1,0); return lastElement; } Array.prototype.pop = array_pop;}
edit: wrapped code in codebox to save space - skemcin