Kayote
Kayote

Reputation: 15647

How To Identify Inefficient Code Within a Function

I was recently asked this question during an interview & apart from console.log & debugger, I wasn't able to give more tools / options for this problem.

The question was: I am reviewing code & I find that the code is causing performance issues. The code is one very lengthy function. How would I go about identifying the line/s of code causing the performance issues.

Thinking about it, now, post the interview, the only other solution that comes to mind is to break the code into tinier functions & analyse. However, I wonder whats the best solution to this problem, not only for the interviews, it would help me be more mindful of options available when I do encounter this problem in real life. (unfortunately, the interviewer was quite non-communicative & clearly wanted to go through the questions as quickly as possible).

Thanks

Upvotes: 1

Views: 186

Answers (1)

Benjamin Racette
Benjamin Racette

Reputation: 172

One easy way to do this is to store the current time right before the infamous function call, then in that function, store the current time before and after strategic places where you think the culprit is. At the end, print the time that it took to execute these parts that are in between the strategic places and you will have a good idea of where the system is spending much of its time.

Upvotes: 1

Related Questions