Console time for Java Script

Hello,

I would like to check the time for some of my function, and use console.time() in Javascript. Is it possible to use this function on the contest ? If yes, how can we implement it ?

Currenlty, I add at the beginning of my function :

console.time();

and at the end :

console.timeEnd();

but I cannot see the time calculated in the log.

Thank you for your feedback.

Viken

Javascrit is running in a Nodejs 12.13.0 env ( https://www.codingame.com/faq ).

According to the documentation, console.timeEnd() will log out in stdout. No really a good thing for a codingame puzzle since we use stderr for debug logs.

I can suggest you to move to the perf_hooks module.

Basic usage:

require { performance } = require('perf_hooks');

const timer = performance.now();

doSomeStuff();

console.error(`Time: ${performance.now() - timer}ms`);

You can also read the documentation to learn how PerformanceEntry and PerformanceObserver works. Pretty useful tool when you have to monitor a Nodejs code performances.

1 Like

Hello,

Thank you Magus, it work perfectly (with one minor update as bellow) :

const { performance } = require(‘perf_hooks’);
const timer = performance.now();
doSomeStuff();
console.error(Time: ${performance.now() - timer}ms);

I found another solution as well, but I don’t know which are the best compared to the previous one :

var begin=Date.now();
doSomeStuffv2();
var end= Date.now();
var timeSpent=(end-begin)+" millisecs";
console.error(timeSpent);

As far as i know, performance.now() is faster than Date.now()

1 Like