What is the space complexity of sum(1)(2)(3)..(n)?
O(n) for the call stack (each call adds a frame). However, since each call returns a new function and the old one is no longer referenced, the GC can free old closures. In practice, the space is close to O(1).
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in sum(1)(2)(3) Complexity and Optimization
O(n) where n is the number of chained calls. Each call creates a new function and performs one addition. The total work is proportional to the number of calls.
Most JS engines (V8, SpiderMonkey) do not implement tail call optimization, even though the ES6 spec includes it. So return sum(a + b) grows the call stack. For very long chains, this could cause stack overflow.
Yes, for a very long chain (e.g., 10,000+ calls). Each call adds a frame to the call stack. Without tail call optimization, the stack grows. In practice, nobody chains 10,000 calls, so this is not an issue.
Still have questions?
Browse all our FAQs or reach out to our support team
