javascript - How can I make this for loop more performant -


i improve loop. ideally not want variable 'nodefound' outside function scope , return 'nodefound' found not after loop has completed.

var nodefound; proto._getnodebyid = function(id, node) {     var data = node || this._data;     var l = data.length;     var i;      ( = 0; < l; i++) {         if (number(id) === data[i].id) {             nodefound = data[i];         } else {             if (data[i].children.length) {                 this._getnodebyid(id, data[i].children);             }         }     }     return nodefound; }; 

proto._getnodebyid = function(id, node) {    var data = node || this._data;   var thisnode = null;   var l = data.length;   var i;    id=+id; // convert number    ( = 0; < l; i++) {       thisnode = data[i] ;       if (id === thisnode.id) {           return thisnode;       } else if (thisnode.children.length) {           thisnode = this._getnodebyid(id, thisnode.children);           if (thisnode) return thisnode;        }           }   return null; }; 

Comments