Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add verbose logging code to functions without affecting performance?

Performance is important for a certain class I'm writing.

I thought about calling a function like so:

debug('This is a debug message, only visible when debugging is on');

And the contents would be like

function debug(message) {
    if (DEBUG) console.log(message);
}

So I wonder: is this enough for V8 to flag this as "dead code" if the DEBUG variable never changes?

Edit: I'm more worried about the performance in Node than on the browser, so removing the code while minifying would be insufficient.

Edit2: I made a JSPerf benchmark out of the proposed solutions, and they are very surprising: http://jsperf.com/verbose-debug-loggin-conditionals-functions-and-no-ops/3

like image 264
skerit Avatar asked Sep 12 '14 09:09

skerit


People also ask

Do console logs affect performance?

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.

What does verbose mean in logging?

In software, verbose logging is the practice of recording to a persistent medium as much information as you possibly can about events that occur while the software runs. It's also worth mentioning that verbose logging is generally a mode that you can toggle on and off.

Does console log affect performance node?

It greatly effects performance. I just did a test with console. log with 60,000 keys in my GAMES object. Then, simply did a for in to check if a property exists while console logging the GAMES object before checking.


1 Answers

I use comments that when a file is minified get removed, such as:

function name(arg) {
    // <debug>
    if (!arg) {
        throw new Error("arg must be defined"); 
    }
    // </debug>
    ... code goes here
}

For example: https://github.com/PANmedia/raptor-editor/blob/master/src/raptor-widget.js#L29-L33

An my (custom) build script to do the aforementioned https://github.com/PANmedia/raptor-build/blob/master/build/raptor-builder.js#L305-L313

like image 134
Petah Avatar answered Oct 02 '22 08:10

Petah