I’m wondering whether investing time into doing tail call optimization for a Google Apps Script is worth it.
So, I’ve understood that Google Apps Script uses the ES2015 (ES6) version of the JavaScript specification (ref1, ref2), running it on the V8 runtime implementation.
Supposedly, ES2015 supports (proper) tail call optimization from the spec. But there are some indications that V8 actually doesn’t implement it:
- What happened to proper tail calls in JavaScript?
- Tail Call Optimization implementation in Javascript Engines
Furthermore, I’ve learned there is an important nuance here:
The terminology of proper tail calls (PTC) and tail call optimization (TCO) is often conflated. Here’s the difference between the two:
- proper tail calls: functions called in the tail position reuse the current stack frame, preventing the creation of additional stack frames that cause space inefficiency.
- tail call optimization: rewrites a recursive function into an iterative one, usually by calling goto.
PTC only deals with stack manipulation, while TCO rewrites a recursive
function as an iterative function.
So, given this…
Does Google Apps Script (GAS):
- support proper tail calls? My guess from the above is “No.”, but it’d be nice to have an authoritative answer, as far as possible.
- support tail call optimization to the point where it is worth doing it, for performance, in any way? My guess is “Yes, you can do it, but it doesn’t improve performance.” But it’d be nice if someone knew it definitively, or could point to a GAS performance comparison that demonstrates it.