Abdu Taviq

How to Find the Exact Line of an Infinite Loop Bug in the Frontend code

- 2 min Read

Video

To play this video, you need to accept the privacy policy.

Video thumbnail

Steps

Example Project Used https://stackblitz.com/edit/js-vub9fd?file=index.js

Performance Tab is recording
Performance Tab is recording

But now you will notice that Chrome freezes, and here is the trick

Performance Tab freezes
Performance Tab freezes

Killing the highest Chrome process
Killing the highest Chrome process

Performance tab is back working
Performance tab is back working

From here we need to find the infinite loop patterns and there are two types of them:

Synchronous Infinite Loops

They look like a long process that takes forever. All you have to do is to find this process and click on it. In the Summary Tab, you can find the location of this method in the file.

Sync Infinite Loop is located
Sync Infinite Loop is located

Asynchronous Infinite Loops

This looks more like infinite inverted pyramids. You will see the call stack is always repeated and the repetition never stops.

You now need to find the connection between the top of a pyramid and the base of the following one and this will direct you to where the infinite loop is happening.

Inverted Pyramids in the Infinite Loop
Inverted Pyramids in the Infinite Loop

Connection between two async functions is found
Connection between two async functions is found

It’s a bit tricky to find and you might spend some time to know why it’s happening but the good thing is you know in what region it is happening in the code.