Scenario: I’m searching for a specific object in a deep object. I’m using a recursive function that goes through the children and asks them if I’m searching for them or if I’m searching for their children or grandchildren and so on. When found, the found obj will be returned, else false. Basically this:
obj.find = function (match_id) {
if (this.id == match_id) return this;
for (var i = 0; i < this.length; i++) {
var result = this[i].find(match_id);
if (result !== false) return result;
};
return false;
}
i’m wondering, is there something simpler than this?:
var result = this[i].find(match_id);
if (result) return result;
It annoys me to store the result in a variable (on each level!), i just want to check if it’s not false and return the result. I also considered the following, but dislike it even more for obvious reasons.
if (this[i].find(match_id)) return this[i].find(match_id);
Btw I’m also wondering, is this approach even “recursive”? it isn’t really calling itself that much…
Thank you very much.
[edit]
There is another possibility by using another function check_find (which just returns only true if found) in the if statement. In some really complicated cases (e.g. where you don’t just find the object, but also alter it) this might be the best approach. Or am I wrong? D:
Although the solution you have is probably “best” as far as search algorithms go, and I wouldn’t necessarily suggest changing it (or I would change it to use a map instead of an algorithm), the question is interesting to me, especially relating to the functional properties of the JavaScript language, and I would like to provide some thoughts.
Method 1
The following should work without having to explicitly declare variables within a function, although they are used as function arguments instead. It’s also quite succinct, although a little terse.
How it works:
this.id == match_id, if so, returnthis.map(viaArray.prototype.map) to convertthisto an array of “found items”, which are found using the recursive call to thefindmethod. (Supposedly, one of these recursive calls will return our answer. The ones which don’t result in an answer will returnundefined.)undefinedresults in the array are removed.undefinedwill be returned.Method 2
Another attempt to solve this problem could look like this:
How it works:
buildObjArraybuilds a single, big, 1-dimensional array containingobjand all ofobj‘s children.filterbased on the criteria that an object in the array must have anidofmatch_id.Both Method 1 and Method 2, while interesting, have the performance disadvantage that they will continue to search even after they’ve found a matching id. They don’t realize they have what they need until the end of the search, and this is not very efficient.
Method 3
It is certainly possible to improve the efficiency, and now I think this one really gets close to what you were interested in.
How it works:
findfunction in atry/catchblock so that once an item is found, we canthrowand stop execution.findfunction (IIFE) inside thetrywhich we reference to make recursive calls.this.id == match_id, wethrow this, stopping our search algorithm.findon each child.throwis caught by ourcatchblock, and thefoundobject is returned.Since this algorithm is able to stop execution once the object is found, it would be close in performance to yours, although it still has the overhead of the
try/catchblock (which on old browsers can be expensive) andforEachis slower than a typicalforloop. Still these are very small performance losses.Method 4
Finally, although this method does not fit the confines of your request, it is much, much better performance if possible in your application, and something to think about. We rely on a map of ids which maps to objects. It would look something like this:
This way, no
findmethod is needed at all!The performance gains in your application by using this method will be HUGE. Please seriously consider it, if at all possible.
However, be careful to remove the object from the map whenever you will no longer be referencing that object.
This is necessary to prevent memory leaks.