Console Log Formatting

Benchmark created on


Description

Compares two options for console log formatting.

Setup

function ts() {
    
       var d = new Date();
       var hrs = '' + d.getHours();
       var min = '' + d.getMinutes();
       var sec = '' + d.getSeconds();
       var mil = '' + d.getMilliseconds();
       
       var pad2 = '00';
       var pad3 = '000';
    
       var str =
          pad2.substring(0, pad2.length - hrs.length) + hrs + ':' +
          pad2.substring(0, pad2.length - min.length) + min + ':' +
          pad2.substring(0, pad2.length - sec.length) + sec + '.' +
          pad3.substring(0, pad3.length - mil.length) + mil + ': ';
    
       return str;
    }
    
    function logFormat(loc, txt) {
       return ts() + txt + ' [' + loc + ']';
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Console with Printf
console.log("%s%s [%s]", ts(), "This is a log message", "myObject.myFunction");
ready
Console with logFormat
console.log(logFormat("myObject.myFunction", "This is a log message"));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.