Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Stouder <xavier@stouder.io>2019-08-21 19:31:06 +0300
committerAnna Henningsen <anna@addaleax.net>2019-08-25 17:39:18 +0300
commit66043e18128dc80457f4e184b7ed7faedcd93ca7 (patch)
tree029a1f6b0529321de21c6422c830e1ce8a5ef5e4 /lib/internal/console
parentb3172f834f418f4f2656d851e585f17aece73333 (diff)
console: display timeEnd with suitable time unit
When timeEnd function is called, display result with a suitable time unit instead of a big amount of milliseconds. Refs: https://github.com/nodejs/node/issues/29099 PR-URL: https://github.com/nodejs/node/pull/29251 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib/internal/console')
-rw-r--r--lib/internal/console/constructor.js32
1 files changed, 29 insertions, 3 deletions
diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js
index a618457124c..554323d9e30 100644
--- a/lib/internal/console/constructor.js
+++ b/lib/internal/console/constructor.js
@@ -32,6 +32,10 @@ const kTraceBegin = 'b'.charCodeAt(0);
const kTraceEnd = 'e'.charCodeAt(0);
const kTraceInstant = 'n'.charCodeAt(0);
+const kSecond = 1000;
+const kMinute = 60 * kSecond;
+const kHour = 60 * kMinute;
+
const {
isArray: ArrayIsArray,
from: ArrayFrom,
@@ -518,14 +522,35 @@ function timeLogImpl(self, name, label, data) {
}
const duration = process.hrtime(time);
const ms = duration[0] * 1000 + duration[1] / 1e6;
+
+ const formatted = formatTime(ms);
+
if (data === undefined) {
- self.log('%s: %sms', label, ms.toFixed(3));
+ self.log('%s: %s', label, formatted);
} else {
- self.log('%s: %sms', label, ms.toFixed(3), ...data);
+ self.log('%s: %s', label, formatted, ...data);
}
return true;
}
+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';
+ }
+
+ return value.toFixed(3) + unit;
+}
+
const keyKey = 'Key';
const valuesKey = 'Values';
const indexKey = '(index)';
@@ -547,5 +572,6 @@ Console.prototype.groupCollapsed = Console.prototype.group;
module.exports = {
Console,
kBindStreamsLazy,
- kBindProperties
+ kBindProperties,
+ formatTime // exported for tests
};