How to optimize a recursive service
Greetings.
I'm trying to build a service to call the BrowseGroups service from the root of a channel and then recursively call it on all of its subgroups (if they're not a System group) until they've all been visited and then return them in an InfoTable.
So, for example, I'd call the service on "HiProfile_LBR" and it would then call itself on "HiProfile_LBR.HiProfile" and so on...

(I collapsed the groups, but the channel in the example has more levels than what's shown in the image.)
This is the code I've written at the moment:
var query = {
"filters": {
"type": "EQ",
"fieldName": "IsSystemGroup",
"value": "true"
}
};
var tempTable, unionParams, groupTable;
var newField = new Object();
newField.name = 'Color';
newField.baseType = 'STRING';
groupTable = Things[indConnector].BrowseGroups({
filter: undefined /* STRING */,
path: path /* STRING */
});
groupTable.AddField(newField);
//-------------------------------------------------------
groupTable = Resources["InfoTableFunctions"].DeleteQuery({
t: groupTable /* INFOTABLE */,
query: query /* QUERY */
});
recBrowse(indConnector, groupTable);
function recBrowse(indConnector, groupTable){
/*let debugJSON;*/
let tableLength = groupTable.rows.length;
for (let x=0; x < tableLength; x++) {
let row = groupTable.rows[x];
if(row.Color != "Black"){
row.Color = "Black";
if(row.HasNonSystemSubGroups){
tempTable = Things[indConnector].BrowseGroups({
filter: undefined /* STRING */,
path: row.FullPath /* STRING */
});
tempTable.AddField(newField);
unionParams = {
t1: groupTable // INFOTABLE,
t2: tempTable // INFOTABLE
};
tempTable = Resources["InfoTableFunctions"].Union(unionParams);
recBrowse(indConnector, tempTable);
}
}
}
}
var result = tempTable;
The service unfortunately times out for complex trees (such as the one in the image above) and I have no idea on how to solve this issue. Besides increasing the service time out (to a value at the moment I don't know), how can I solve the issue? How would you optimize my code?
Thank you.

