0

The setTimeout call removes the function from the call stack and adds it to the settimeout API. Otherwise, the recursive function creates a stack overflow. What is actually going on under the hood?

const list = new Array(60000).join('1.1').split('.');

function removeItemsFromList() {
    var item = list.pop();

    if (item) {
      setTimeout(removeItemsFromList, 0);
    }
};

removeItemsFromList();
FitAdam
  • 49
  • 6
  • "*What is actually going on under the hood?*" -> "*The setTimeout call removes the function from the call stack and adds it to the settimeout API.*" Also see my answer to [Understanding Event Queue and Call stack in javascript](https://stackoverflow.com/q/39459236) – VLAZ Aug 24 '22 at 13:24
  • @VLAZ The answer describes the process generally but I'm trying to understand the limitations of the so-called Event Loop. – FitAdam Aug 24 '22 at 14:31
  • 1
    That's not what your question here asks. Limitation in terms of what? The title asks about a memory limit but that makes little sense. If you're asking "would this code lead to a memory leak" then - no, because there is no extra memory consumed. The function is either enqueued or not. If you're asking about CPU consumption, then also no. An enqueued item does not have any CPU overhead. Speed of execution would be different but that's because there is a minimum waiting time. So running `list` is empty would take longer but the time spent running `removeItemsFromList` would be a smaller portion. – VLAZ Aug 24 '22 at 14:49

1 Answers1

1

To answer your question which is :

Does the set timeout web API have no memory limit?

I would say from what I found searching around to help you is that you may run into the problem of memory limitations available to node with setTimeOut function.

Read more about it here