Google Drive folders properties caching to file I have the recursive function below to cache all the folders of my company Drive into a file. It works like a charm but takes too long, specifically >30 secs to read and save the 300 folders.

What I need is to make it faster somehow. I don't know if it is possible to make the folder reading asynchronous, or if it possible to make a webhook or something to listen to changes in the Drive's folders, which would eliminate the need to run recursively over all folders.

function generateFolderTree( idParent, tree ){
  var currParent = DriveApp.getFolderById( idParent),
      folderChildren = currParent.getFolders(),
      currChild,
      currChildId;

  if( tree.length == 0 ){
    tree.push({ 'text' : currParent.getName(), 'parent' : '#', 'id' : idParent, 'state' : { 'opened' : true }, 'li_attr':{'title': ''} });
  }

  while( folderChildren.hasNext() ){
    currChild = folderChildren.next();
    currChildId = currChild.getId();

    tree.push({ 'text' : currChild.getName(), 'parent' : idParent, 'id' : currChildId, 'li_attr':{'title': currChild.getDescription()} });

    generateFolderTree( currChildId, tree );
  }
}

function geneteAndSaveTheTree(){
  var theTree = [];

  generateFolderTree(folderToIterateId, theTree);

  DriveApp.getFileById(theFileWhereAllFoldersAreStored).setContent(JSON.stringify(theTree));
  return theTree;
}


I know you can get all folders at once for a Drive, but I think it would take even longer to find out all the folders that are child of the folderToIterateId.
Other than the fact that it's hard to read because your variables are not in Englishref, your code appears solid. Two observations:


Some comments would be beneficial, even if only function, parameter and return descriptions. Use of recursion should be clearly explained in comments, for the next poor soul who needs to figure out why they ran out of memory.
You have not explicitly handled the case of an invalid folder ID parameter in gerarJqTree().


The only optimization I'd suggest would be to do what you think would take longer; get all folders at once - but do it with the advanced Drive service so you have the container information as well. While you'll need to traverse the returned items to construct the output, you will eliminate several hundred service calls.