Home > front end >  Is console.debug faster than console.log?
Is console.debug faster than console.log?

Time:12-16

Here's what I tried:

const log = () => {
    console.time("Log");
    console.log("Test");
    console.timeEnd("Log");
}

const debug = () => {
    console.time("Debug");
    console.debug("Test");
    console.timeEnd("Debug");
}

When I run these results are as follows:

Test
VM406:4 Log: 0.06396484375 ms
Test
VM500:4 Debug: 0.059814453125 ms

And most of the time I see debug having a slight edge on log. Is it just co-incidence or debug is actually faster than log? And if so should we just stop using log at all cases and use debug instead?

CodePudding user response:

No, the log level is handled as a string within the console object, and there is no reason to think that debug is faster than log.

The fact that trying to execute your code is likely to reproduce the output is due to the fact that log is executed before debug, and I can suppose that most of the internal objects of console gets cached by the JavaScript engine. For this reason, the second execution will always be a bit faster than the first

const log = () => {
    console.time("Log");
    console.log("Test");
    console.timeEnd("Log");
}

const debug = () => {
    console.time("Debug");
    console.debug("Test");
    console.timeEnd("Debug");
}

log();
debug();

const log = () => {
    console.time("Log");
    console.log("Test");
    console.timeEnd("Log");
}

const debug = () => {
    console.time("Debug");
    console.debug("Test");
    console.timeEnd("Debug");
}

debug();
log();

CodePudding user response:

No, console.debug is not always faster than console.log.

The speed difference is minimal, and it depends on the environment and operating systems used. Therefore, console.log is still the most popular method for displaying messages in the console and remains a good choice for most use cases.

I personnaly always use console.log.

  • Related