JavaScript Error Handling

JavaScript Error Handling – RangeError: repeat count must be less than infinity

Today as we plow ahead through our JavaScript Error Handling series we’re going to be tackling the Infinite Repeat Counterror. The Infinite Repeat Count error occurs during the use of the repeat() method of a String object in JavaScript, and specifically when the count parameter passed to that method is too large.

Below we’ll take a look at a few specific examples seen in the wild which can raise an Infinite Repeat Count error, and also explore how to deal with this error when it pops up. Let’s get to it!

The Technical Rundown

  • All JavaScript error objects are descendants of the Error object, or an inherited object therein.
  • The RangeError object is inherited from the Error object.
  • The Infinite Repeat Count error is a specific type of RangeError object.

When Should You Use It?

As mentioned in the introduction, the Infinite Repeat Count error is quite a rather specific error that only rears its ugly head in a handful of situations. The real clue is in the second word in the name, repeat, which hints that the Infinite Repeat Counterror will occur only when the repeat() method is used on a String object. Furthermore, the Infinite Repeat Count error will then only pop up when the first (and only) parameter, count, is provided and is given a value of either Infinity or a value which would otherwise cause the resulting String to exceed the maximum size limitation within the executing JavaScript engine (depending on the browser).

Let’s try it out with a simple example and see if we can’t produce our very own Infinite Repeat Count error. Here we’re creating a few variables: count to represent the number of repetitions of our String, and name to hold the actual string value. Then we call the repeat() method and attempt to catch our expected RangeErrors using a simple printErrorfunction to format the error output:

As mentioned above, the Infinite Repeat Count error can behave slightly differently depending on the JavaScript engine in use. In this case we’re testing with both Chrome and Firefox, and both produced an Infinite Repeat Count error but the message of the error itself differs significantly:

Now, setting our count value to Infinity seems like a cheap trick to trigger an Infinite Repeat Count error, so let’s try just a very large value to see where we can exceed the String size limits of the JavaScript engine.

Here we’ve change name to be only a single character long so the math is simplified, so we know that every repeat from our count variable is just adding one single character to our string. Unfortunately, a String of length 228 is too large for both Firefox and Chrome:

Notice that Chrome’s engine recognized that the issue wasn’t actually an invalid count value passed to the repeat()method, but that the resulting string length was too large.

In this case, the max size for our count value for Firefox would be 228-1 and 227 for Chrome, although this is likely to dramatically increase in future browser versions as the usage of the latest JavaScript, ECMA 6.0, becomes more prevalent.

To dive even deeper into understanding how your applications deal with JavaScript Errors, check out the revolutionary Airbrake JavaScript error tracking tool for real-time alerts and instantaneous insight into what went wrong with your JavaScript code.