diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-09-04 20:08:56 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-10-01 21:13:53 +0200 |
commit | 204248a0c3597b99dc4a706203292141fbaf85ed (patch) | |
tree | e59c5cf6a3c93e28ae15eb45e9434ac37f3f55b3 /lib | |
parent | 3473e58fee153a7fee143a33c512ee9cda08d20a (diff) | |
download | android-node-v8-204248a0c3597b99dc4a706203292141fbaf85ed.tar.gz android-node-v8-204248a0c3597b99dc4a706203292141fbaf85ed.tar.bz2 android-node-v8-204248a0c3597b99dc4a706203292141fbaf85ed.zip |
console: update time formatting
This improves the readability of the `console.timeEnd()` output
while keeping a higher output's precision in multiple cases.
Instead of e.g. '1.005min' it will print '1:00.300 (m:ss.mmm)'.
PR-URL: https://github.com/nodejs/node/pull/29629
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/console/constructor.js | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 554323d9e3..c3339241aa 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -3,7 +3,7 @@ // The Console constructor is not actually used to construct the global // console. It's exported for backwards compatibility. -const { Object, ObjectPrototype, Reflect } = primordials; +const { Object, ObjectPrototype, Reflect, Math } = primordials; const { trace } = internalBinding('trace_events'); const { @@ -533,22 +533,38 @@ function timeLogImpl(self, name, label, data) { return true; } +function pad(value) { + return `${value}`.padStart(2, '0'); +} + function formatTime(ms) { - let value = ms; - let unit = 'ms'; - - if (ms >= kHour) { - value = ms / kHour; - unit = 'h'; - } else if (ms >= kMinute) { - value = ms / kMinute; - unit = 'min'; - } else if (ms >= kSecond) { - value = ms / kSecond; - unit = 's'; + let hours = 0; + let minutes = 0; + let seconds = 0; + + if (ms >= kSecond) { + if (ms >= kMinute) { + if (ms >= kHour) { + hours = Math.floor(ms / kHour); + ms = ms % kHour; + } + minutes = Math.floor(ms / kMinute); + ms = ms % kMinute; + } + seconds = ms / kSecond; + } + + if (hours !== 0 || minutes !== 0) { + [seconds, ms] = seconds.toFixed(3).split('.'); + const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes; + return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`; + } + + if (seconds !== 0) { + return `${seconds.toFixed(3)}s`; } - return value.toFixed(3) + unit; + return `${Number(ms.toFixed(3))}ms`; } const keyKey = 'Key'; |