Skip to content

Allow customizable time units in console.time #29099

@timendez

Description

@timendez

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    consoleIssues and PRs related to the console subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions