Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizable time units in console.time #29099

Closed
timendez opened this issue Aug 12, 2019 · 9 comments
Closed

Allow customizable time units in console.time #29099

timendez opened this issue Aug 12, 2019 · 9 comments
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@timendez
Copy link

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!

@juanarbol
Copy link
Member

I'm not an expert, but I think that globals such as console should behave as web does, if you see console timers docs, time is considered on milliseconds, and I'm not sure of your alternative, Javascript numbers are double-precision 64-bit floating point, maths operations sometimes are not very precise, I do not see this very convenient; also, milliseconds is not a complex unit to convert.

Let me know what do you think

@devsnek devsnek added console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js. labels Aug 13, 2019
@devsnek
Copy link
Member

devsnek commented Aug 13, 2019

We can't add new parameters to console methods, but the time format is implementation defined. We could make it more human readable based on the duration. I would be fine with something like 3.38h (12168532.35ms).

@devsnek
Copy link
Member

devsnek commented Aug 13, 2019

cc @nodejs/console

@timendez
Copy link
Author

@devsnek That sounds wonderful to me! Thanks

@nvispute
Copy link

nvispute commented Aug 13, 2019

Have you tried time-span & time-stamp npm packages..

@timendez
Copy link
Author

@nvispute It looks like time-span would do what I want. However, I would much rather use what I listed under Current Alternative than introduce another dependency - especially from a largely unknown origin

@nvispute
Copy link

Well its always a manual v/s a package :P no middle way to it.. and the alternative you listed is something i have been seeing since i start to code :) so i believe that would only be the way until something comes ups when it comes up

@timendez
Copy link
Author

@nvispute What I'm suggesting as a feature is to get around the manual way and the package way - to just include it with Node.js

addaleax pushed a commit that referenced this issue Aug 25, 2019
When timeEnd function is called, display result with a suitable
time unit instead of a big amount of milliseconds.

Refs: #29099

PR-URL: #29251
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
BridgeAR pushed a commit that referenced this issue Sep 3, 2019
When timeEnd function is called, display result with a suitable
time unit instead of a big amount of milliseconds.

Refs: #29099

PR-URL: #29251
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@Xstoudi
Copy link
Contributor

Xstoudi commented Oct 24, 2019

As it landed with v13.0.0, I think this issue can be closed.

@targos targos closed this as completed Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

6 participants