Console or print statements are killing your app’s performance.

Theo Okafor
3 min readJan 21, 2021

--

burning house

“Remove all unnecessary console or printstatements from your code base before pushing to production.”

This statement sounds cliché. You might have said it to someone before. You know that it is important, but you do not know why. I discovered the reason by accident and in the next couple of paragraphs, I am going to blow your mind.

The console or print statements has a mind-boggling impact on performance.

The harmless-looking console.log or print statements littered in your code base has a serious impact on your app’s performance. Allow me to show this with codes.
JavaScript (Node.js) Example

Code for testing the runtime with and without console.log in JavaScript(Node.js)

In the code above we have two functions testRuntimeWithConsole and testRuntimeWithoutConsole. The functions work exactly the same. They both sum the numbers from 1 to 99. The only difference is that testRuntimeWithConsole has a console.log statement that outputs the current sum at every iteration. Below is the screenshot of the result.

the result from running the node.js/JavaScript code on the console
the result from running the Node.js/JavaScript code on the console

Wait, what? The one with console.log took 7.762 milliseconds to run, while the one without console.log took 0.011 milliseconds! If you’re thinking: This can can’t be right. It’s probably a JavaScript/Node.js thing. Well, I thought you would think that, so I tried it with Python too. Here is the code:

Python3 Example

Code for testing the runtime with and without console.log in Python3

Like the JavaScript one, test_runtime_with_print and test_runtime_without_print are the two functions to run. They both sum the numbers from 1 to 99. The only difference is that test_runtime_with_print has a print statement that outputs the current sum at every iteration. Here is the result below:

the result from running the python code
the result from running the python code

This is insane!

The function with aprint statement ran for 0.455 milliseconds, while the one without print ran for 0.009 milliseconds.

Why does this happen?

Let me try to answer this. My explanation is that writing/outputting anything to the console is part of IO stream. The operating system has to do all the work it takes to get each piece of information from your code to the console/screen. More work means more time taken. Also the runtime will vary from one operating system to another. I ran this using Linux/Ubuntu. You can try the codes on your machine and tell me what you find.

What can you do going forward?

The answer is the same as before; Remove all unnecessary console or print statements.

It turns out that just one or two of them is enough to cause a problem too. Recently, I had a timeout issue on Hackerrank while solving one of their algorithm challenges. The last test was failing due to timeout. For many minutes I fiddled around without a result, trying to improve the runtime. I got a hunch to remove the two console.log statements in my code(which were not even in a loop). Surprisingly, that solved the timeout problem.

You have made it this far, well done!

An ask from me

If you found this article helpful, please knack/whack that clap button until it stops functioning 😄. If you have a contribution, comment, or anything, please post it down below, I’ll appreciate it. Cheers!

Follow me on Twitter via @theo_dot_io.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Theo Okafor
Theo Okafor

Written by Theo Okafor

Software Engineer. Loves to play Coldplay on repeat while coding JavaScript and Python apps.

No responses yet

Write a response