Imagine I have this hypothetical function (in javascript):
function paginationRecursive(results) {
let intermediate = retrieveNextPage();
results = results.concat(intermediate)
if (haveAllResults()) return results;
return paginationRecursive(results);
}
Will each value of intermediate
be kept in memory until the whole recursive processing has completed, thus everytime increasing memory usage for every recursive call? Or is the engine/gc smart enough to free this memory at any time we go one call "deeper", because it knows that the variable will never be used again?
It could be that it is engine specific (e.g. V8, SpiderMonkey, Chakra, etc.) so in order to make this question not to broad, I prefer to know the answer for the V8 engine.