The Problem
I am trying to output time taken in seconds using the built in console.time() and console.timeEnd() functions.
Currently, my code and output looks like this:
console.time('Time taken');
// do things
console.timeEnd('Time taken');
Output: Time taken: 34239.098ms
The Solution
I would like to be able to pass in an optional parameter to console.timeEnd() to be able to output the results in different units of time rather than milliseconds.
Example:
console.time('Time taken');
// do things
console.timeEnd('Time taken', 's'); // or 'ms', 'm', 'h', 'd'...
Output: Time taken: 34.239s
Current Alternative
This is the best alternative that I've currently found:
const begin = Date.now();
// do things
const end = Date.now();
const elapsedTime = (end-begin) / 1000;
console.log(`Time taken: ${elapsedTime}s`);
Output: Time taken: 34.239s
Conclusion
Normally I do use milliseconds with my output - and it should remain the default. However I have come across numerous asynchronous processes I'd like to easily measure final time for. Reading 12168532.35ms instead of 3.38h is slightly more difficult.
Thanks for your consideration!
The Problem
I am trying to output time taken in seconds using the built in
console.time()andconsole.timeEnd()functions.Currently, my code and output looks like this:
Output:
Time taken: 34239.098msThe Solution
I would like to be able to pass in an optional parameter to
console.timeEnd()to be able to output the results in different units of time rather than milliseconds.Example:
Output:
Time taken: 34.239sCurrent Alternative
This is the best alternative that I've currently found:
Output:
Time taken: 34.239sConclusion
Normally I do use milliseconds with my output - and it should remain the default. However I have come across numerous asynchronous processes I'd like to easily measure final time for. Reading 12168532.35ms instead of 3.38h is slightly more difficult.
Thanks for your consideration!