Optimizing search by id in some data in JSON structure I have an object created by a JSON data. I need to retrieve the id for an object, I would like to know if a faster way exist, considering I am using for ..in loop.

var test =  {
     id: 0,
      cnt: [
        {
          id: 1,
          cnt: [
            {
              id: 2,
              cnt: [
                {
                  id: 3,
                  cnt:[]
                }
              ]
            }
          ]
        },

      ]
    };



    getById: function (id) {
                function getByIdInner(obj, id) {
                    var result;
                    for (var p in obj) {
                        if (obj.id == id) {
                            return obj;
                        } else {
                            if (typeof obj[p] === 'object') {
                                result = getByIdInner(obj[p], id);
                                if (result) {
                                    return result;
                                }
                            }
                        }
                    }
                    return result;
                }
                return getByIdInner(test, id);


   },
It looks like this is just a snippet of the code you are using, but if this is the standard structure, you could simplify your search by doing something like:

getById: function(id) {
    function innerGetById(obj, id) {
        var result;
        if (obj.id == id) {
            return obj;
        } else {
            for (var i = 0; i &lt; obj.cnt.length; i++) {
                result = innerGetById(obj.cnt[i], id);
                if (result) {
                   return result;
                }
            }
        }
    }
    return innerGetById(test, id);
}


This will only work if you know the names of the keys you are dealing with, if that is unknown, you will need to do a for in loop like you have. The key here is that you are testing for the id outside of the loop through the array.

I would also define innerGetById outside of your getById function if possible. I just wrote it this way because I don't know the context that you are defining the getById function in and it looks like you may be creating it as part of an object.

To answer your question, I'm not sure that a faster way exists than manually searching through the structure unless you are willing to index the structure before doing the search. If you want to do that, you can use the same sort of recursive method, but create a flat object with the ids as keys and the associated object as the value. This will make lookups very simple.

You can turn your test object into something like this:

lookupTable = {
    0: { id: 0, cnt: [ ... ] },
    1: { id: 1, cnt: [ ... ] },
    2: { id: 2, cnt: [ ... ] },
    etc.
}


By using something like:

var lookupTable = {};
function buildLookupTable(obj) {
    lookupTable[obj.id] = obj;
    for (var i = 0; i &lt; obj.cnt.length; i++) {
         buildLookupTable(obj.cnt[i]);
    }
}
buildLookupTable(test); // Using your original test data


Then your lookup is simply: lookupTable[id]