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?
>Solution :
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();