console.log Will Cost You Browser Ops

Writing log statements in your javascript code is a habitual practice for web developers. But if you do not comment them out you might be giving up performance for nothing. This is the single most simple tweak you can do to your code to speed up javascript execution. Remember to comment out those log statements before you push code to production. This is a bigger problem for mobile browsers because every browser operation is important with that limited processing power your app gets.

To see how leaving console.log statements in your code can affect performance. Look at this jsperf test I wrote.

2 thoughts on “console.log Will Cost You Browser Ops

  1. Thank you for pointing this out and spreading the word against a sea of nay-sayers who naively think executing additional code won’t cause additional CPU consumption.

    It’s very obvious console.log causes a performance hit if you actually do serious coding, so I’m at a loss for why some people choose not to see it. My best guess is they’re either not very perceptive, or don’t know how to drill down to actually shining a spotlight on it and testing.

    Just throw a console.log into any intensive loop (frame updates, mouse movements or what have you) and if enough else is going on, there will be noticeable performance drops to the end-user.

    Six years after you wrote this, I still see people religiously believing console.log lives in a magic realm of fairy code that is executed not on CPU cycles but fairy dust and then warped into our realm so we don’t have to process the code it executes.

    It’s almost like a little critical thought is lacking in some people. -_-


    1. Wow! 6 years is a long time to remember the mind set that I had when I wrote this article. But even in today’s world, this is very much valid. The biggest thing people don’t seem to get is that JS is single-threaded. There’s the Service Worker now, which allows you to do processing off the main thread but that’s a whole another topic of its own.

      It is always nice to see relevant comments on articles, even 6 years later. I am glad that you read this.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.