Skip to content Skip to sidebar Skip to footer

Is There A Limit To How Many Levels You Can Nest In Javascript?

Say you have this really complex algorithm that requires dozens of for loops. Does JavaScript have a limit to how deep loops can be nested or is there no limit? What is the best pr

Solution 1:

There's no limit in the specification. There will probably be a limit in any implementation due to memory/stack overflows...

For example, this works fine:

var s = 0;
var is = newArray(11);

for(is[0] = 0; is[0] < 2; is[0]++) {
  for(is[1] = 0; is[1] < 2; is[1]++) {
    for(is[2] = 0; is[2] < 2; is[2]++) {
      for(is[3] = 0; is[3] < 2; is[3]++) {
        for(is[4] = 0; is[4] < 2; is[4]++) {
          for(is[5] = 0; is[5] < 2; is[5]++) {
            for(is[6] = 0; is[6] < 2; is[6]++) {
              for(is[7] = 0; is[7] < 2; is[7]++) {
                for(is[8] = 0; is[8] < 2; is[8]++) {
                  for(is[9] = 0; is[9] < 2; is[9]++) {
                    for(is[10] = 0; is[10] < 2; is[10]++) {
                      s++;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

document.write(s);

Solution 2:

Just to kick in with a short test that I've since modified to not count loops as much as see the memory limit... This code (please don't use it, your machine will hate you):

functionx() {
    functionnewLoop(index) {
        var y = [];
        console.log("index");
        for (i = index; i < index+1000; i++) {
            y.push(i);
            if(i == index+999) {
                console.log(i);
                newLoop(i);
            }
        }
    }
    newLoop(0);
}
x();

has stopped at logging 499500 to the console. That is probably hitting some safety switch or the memory limit.

That's 500 nested loops.

In an earlier test with a lighter version of this code I've gotten up to 999 nested loops in just the first second, with the code clogging up my browser for another few seconds (but not displaying the rest because of "too many messages per second to the console" error).

I didn't care enough too bother with more details after that nor do I see benefits of a more detailed description here, but (in my project) I'm traversing HTML with a whole lot of nested loops in badly laid out pages and these results faaar exceed my needs.

TL;DR: memory plays a bigger role than the number of loops, but I've gotten above a 1000 nested loops. Please don't ever use that many though :)

PS. this was run in Edge, for the version check the date of my post :)

Solution 3:

There is no maximum nesting level that you should worry about when you are writing code that a human is supposed to read and maintain. You can nest hundreds of loops without problems.

However, you should avoid it wherever possible! At some point someone will have to understand your code (most likely it's you, when you are debugging!) and will curse you. It should be possible to extract inner loops into a separate function with a meaningful name.

Post a Comment for "Is There A Limit To How Many Levels You Can Nest In Javascript?"