Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google script (JS) - maximum recursion depth

What is the maximum recursion depth in Google Apps Script scripts? I have a function, match_recurse, which looks like the following pseudocode:

function match_recurse(array) {
  for (i=0, i<3, i++) {
    var arr2 = array.copy().push(i);
    if (is_done(arr2)) break;
    match_recurse(arr2);
  }
}

(It also returns its results, but I don't want to bloat the question.)

Now, because the execution errored, the execution transcript and the logs were not saved, so I have no way of knowing whether my is_done function is doing its job wrong. I can do a few cases of the problem on paper and check recursion depth, but I don't know what the maximum is supposed to be.

Looking on the web, I saw an article mentioning that IE has a max call stack of 13 if you go through the Window object, but nothing else.

like image 251
Riking Avatar asked Oct 18 '12 01:10

Riking


2 Answers

It is 1000, as one can see from here:

function recurse(i) {
  var i = i || 1;
  try {
    recurse(i+1);
  } catch (e) {
    Logger.log(i);
  }
}
like image 198
Corey G Avatar answered Sep 20 '22 00:09

Corey G


The stack depth value is not documented. Executing the following code shows that this value is equal to 1000.

function getStackDepth(curvalue) {
  try {
    curvalue = getStackDepth(curvalue) + 1;
  }
  catch(err) {
  }
  return curvalue;
}

function test() {
  var depth = getStackDepth(2);
  debugger;
}
like image 25
megabyte1024 Avatar answered Sep 18 '22 00:09

megabyte1024