148

Will use of the debugging feature console.log reduce JavaScript execution performance? Will it affect the speed of script execution in production environments?

Is there an approach to disable console logs in production environments from a single configuration location?

4
  • 2
    All the answers, so far, assume you are simply outputting string messages. What about the performance of logging objects, with possibly large object structures? eg console.log(largeObj)? Commented Nov 3, 2016 at 4:18
  • 2
    Outputting significant numbers of objects to console can turn 3 second page load into 30 seconds. Just an example... Commented Mar 21, 2017 at 16:37
  • 1
    A simple console.log takes ~50ms Commented Aug 13, 2019 at 3:58
  • This whole thread reeks of premature optimization. Commented Dec 7, 2022 at 18:22

12 Answers 12

113

Actually, console.log is a lot slower than an empty function. Running this jsPerf test on my Chrome 38 gives stunning results:

  • when the browser console is closed, calling console.log is about 10 000 times slower than calling an empty function,
  • and when the console is open, calling it is as much as 100 000 times slower.

Not that you'll notice the performance lag if you have a reasonable number of console.… calls firing once (a hundred will take 2 ms on my install of Chrome – or 20 ms when the console is open). But if you log stuff to the console repeatedly – for instance, hooking it up through requestAnimationFrame – it can make things janky.

Update:

In this test I've also checked out the idea of a custom “hidden log” for production – having a variable which holds log messages, available on demand. It turns out to be

  • about 1 000 times faster than the native console.log,
  • and obviously 10 000 times faster if the user has his console open.
Sign up to request clarification or add additional context in comments.

14 Comments

When a compiler sees an empty function, it executes nothing wether as it sees a line to execute it will have to run the function. the compiler simply doesn't run an empty, unused function as an optimisation.
@SidneyLiebrand thanks for the info, that’s good to know. The results of the second test can be optimized in the same way as console.log though. Both of them are functions which produce side-effects.
console.log by itself doesn't really impact performance in a way that you'll notice unless you bind it to a scroll / resize handler. These get called alot and if your browser has to send text to the console like 30/60x a second it can get ugly. And then there's that IE bug that didn't allow you to have console.log's at all with the console closed :(
You’re absolutely right – I wrote that in my answer too. As a rule of thumb, I try to have no console calls in production code. But performance is not the reason – it’s rather because “its too easy for a layman to read your logs” as @Ramesh wrote.
4 years later (checking @Ĭsααctիεβöss's comment), the links are still broken.
|
78

If you are going to have this on a public site or something, anyone with little knowledge on using the developer tools can read your debug messages. Depending on what you are logging, this may not be a desirable behavior.

One of the best approaches is to wrap the console.log in one of your methods, and where you can check for conditions and execute it. In a production build, you can avoid having these functions. This Stack Overflow question talks in details about how to do the same using the Closure compiler.

So, to answer your questions:

  1. Yes, it will reduce the speed, though only negligibly.
  2. But, don't use it as it's too easy for a person to read your logs.
  3. The answers to this question may give you hints on how to remove them from production.

13 Comments

Just a note - using console.log to log objects does cause memory leak as browser retains the object structure to allow developers to expand the log.
"it's too easy for a person to read your logs" — How is this a problem? It's JavaScript, they already have full access to the source code!
@BluDragn — Why are you sending personal customer data, which you don't trust the customer with, to the customer? Even without console.log they can just look in the Network tab.
Just to chime in: I just discovered, spamming the console.log even with the same static text (no object or array; literally just console.log('test') would do it) also causes a performance hit. This came to my attention as I was logging "called" within a function that got invoked on hundreds of React components during re-renders. The mere presence of the logging code caused very noticeable stutter during frequent updates.
|
30
const DEBUG = true / false
DEBUG && console.log('string')

3 Comments

what an elegant solution!
missing semi-colons ;)
Came here wondering the same thing since I’ve been cleaning out console.logs littered over my latest project and was wondering how much actual impact they have, provided you aren’t dumping huge amounts of data. This is a clever solution, and would be a no-brained to hook to the env variable you often have for logging and any other constants that differ between dev and prod environments. :)
20

Will use of the debugging feature console.log reduce JavaScript execution performance? Will it affect the speed of script execution in production environments?

Of course, console.log() will reduce your program's performance since it takes computational time.

Is there an approach to disable console logs in production environments from a single configuration location?

Put this code at the beginning of your script to override the standard console.log function to an empty function.

console.log = function () { };

1 Comment

This seems to be one of the simplest solutions for disabling console logs in production environment. Wondering why it does not have more attention! We can control the definition of this empty function under a variable which we can toggle as true/false depending on which environment we are working on (prod or dev)
13

If you create a shortcut to the console in a common core script, eg:

var con = console;

and then use con.log("message") or con.error("error message") throughout your code, on production you can simply rewire con in the core location to:

var con = {
    log: function() {},
    error: function() {},
    debug: function() {}
}

1 Comment

the dirty way: console.log = function(){}
8

Any function call will slightly reduce performance. But a few console.log's should not have any noticeable effect.

However it will throw undefined errors in older browsers that don't support console

Comments

4

The performance hit will be minimal, however in older browsers it will cause JavaScript errors if the users browsers console is not open log is not a function of undefined. This means all JavaScript code after the console.log call will not execute.

You can create a wrapper to check if window.console is a valid object, and then call console.log in the wrapper. Something simple like this would work:

window.log = (function(console) {
    var canLog = !!console;
    return function(txt) {
        if(canLog) console.log('log: ' + txt);
    };
})(window.console);

log('my message'); //log: my message

Here is a fiddle: http://jsfiddle.net/enDDV/

Comments

3

I made this jsPerf test: http://jsperf.com/console-log1337

It doesn't seem to take any longer than other function calls.

What about browsers that doesn't have a console API? If you need to use console.log for debugging, you might include a script in your production deployment to override the console API, like Paul suggests in his answer.

4 Comments

if i can select two answers this will go to top
Your test not only adds a console.log call, but also execute the same jquery operation twice. I've created the following revision of your test, hope it helps: jsperf.com/console-log1337/7 PS: thanks, I didn't know about jsperf.com :)
It actually does seem to be a lot slower than a normal function call. In a direct duel the results are incomparable: jsperf.com/console-log1337/14
Bad answer. Not even remotely correct. Wishful thinking up-votes as @tomekwi demonstrates the difference is remarkable. I've done several real world tests myself and can absolutely say without a shadow of a doubt spamming console.log definitely causes a performance hit. A few logs here and there every second or two, no big deal, but log something frequently (on frame updates, re-renders of massive components) and the difference with and without console.log is night and day.
1

I do it this way to maintain original signature of console methods. In a common location, loaded before any other JS:

var DEBUG = false; // or true 

Then throughout code

if (DEBUG) console.log("message", obj, "etc");
if (DEBUG) console.warn("something is not right", obj, "etc");

Comments

1

I prefer it doing this way:

export const println = (*args,) => {
   if(process.env.NODE_ENV === "development") console.log(args)
}

and then use println everywhere in your code.

Comments

0

You can also test if if you're on production before replacing console.log with a empty function: (example on Vue)

//* disable console.log on production: 
if (import.meta.env.MODE === 'production') console.log = function () {};

Comments

0

Create a wrapper function to use for logging which takes the environment into consideration. Lots of room for improvement on this example wrapper function, but the point is you have the ability to update your logging logic in a single place rather than spread throughout the code base.

const DEBUG = true

const logger = (level, message) => {
  if (!DEBUG) return
    if(level="log") console.log(message);
    else if (level="warn") console.warn(message);
}

logger("log", "This is me log message")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.