Categories
JavaScript Answers

How to Fix the ‘InternalError: too much recursion’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘InternalError: too much recursion’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘InternalError: too much recursion’ when we’re developing JavaScript apps.

Fix the ‘InternalError: too much recursion’ When Developing JavaScript Apps

To fix the ‘InternalError: too much recursion’ when we’re developing JavaScript app, we should make sure that our recursion has a base case that’s executed.

And when that’s executed, the recursion code stops running.

The ‘InternalError: too much recursion’ error is fired by Firefox when it tries to run infinite recursion code.

In Edge, the Error: Out of stack space error is fired.

And in Chrome, the RangeError: Maximum call stack size exceeded is fired for the same error.

Example, of infinitely recursive code is something like:

const count = (x) => {
  count(x + 1);
}
count (0);

There’s no base case in this code, so count keeps running forever once it’s called.

The error can also be fired when there’s too much recursion in the code.

For instance, if we have:

const count = (x) => {
  if (x >= 1000000000000) {
    return;
  }
  count(x + 1);
};
count(0);

Then count stops running only when x is 1000000000001.

This is too much for the browser to handle, so the browser will fire the error.

Also, we should make sure we don’t have setters that sets the same value as it’s getting.

For instance, if we have:

class Person {
  constructor() {}
  set name(name) {
    this.name = name;
  }
}

Then we also have this error because we’re setting and getting the name property in the name setter.

Conclusion

To fix the ‘InternalError: too much recursion’ when we’re developing JavaScript app, we should make sure that our recursion has a base case that’s executed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *