I've seen code on an inherited project output debug info while iterating large collections of objects.

System.debug(Logginglevel.DEBUG, 'sQuery=' + sQuery);

If the system logging level is set higher than DEBUG in the above example, how expensive is something like this to keep in the code?

Are there any short-circuits that prevent the construction of two String objects above to construct the message that may be output?

Best practices on leaving junk like this in production codebases?


1 Answer 1


I have seen too many debugs double the processing time and at times cause the apex time limit exceeded. There is a cost to everything we do.

In addition writing


is less costly than:

system.debug('Key: ' + k + ' - Map Value: ' + map.get(k));

The exact cost depends on how many debugs, are they in a loop, and exactly what / how they are debugging.

If you want to see, take any code with debugs, execute it with the console open, and check out the Execution Overview - Timeline.

Repeat with the debugs commented out.

Do that several times to average out system delays and you will see the primate cost of the debugs in your class

As to your last question:

Directly - No

Indirectly - you can have a global custom setting that you can check to enable debug statements. Then you can conditionally output the debugs. This would require you to update your code to check the custom setting value.

This will reduce the cost during runtime and still allow you to "flip a switch" to get the debug output. There is still a small cost for the constant evaluation of the flag though.

  • Someone should write a set of ant tasks to programmatically comment out all debug lines and just incorporate that into an Ant build file using the SF Deployment/Migration tool. Oct 24, 2016 at 20:08
  • 1
    a hierarchical custom setting that allows one to enable logs for specific running users/profiles can be especially handy
    – cropredy
    Sep 13, 2018 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .