Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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 '1m and 300ms'.
  • Loading branch information
BridgeAR committed Sep 23, 2019
commit 1dcb3d1a4ee358ca718a0e0e1d0de50cae100cab
47 changes: 35 additions & 12 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -534,21 +534,44 @@ function timeLogImpl(self, name, label, data) {
}

function formatTime(ms) {
let value = ms;
let unit = 'ms';
let end = 3;
const values = [];

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';
end = -1;
values.push(`${Math.floor(ms / kHour)}h`);
ms = Math.floor(ms % kHour);
}
if (ms >= kMinute) {
if (end === 3) {
end = 0;
}
values.push(`${Math.floor(ms / kMinute)}min`);
ms = ms % kMinute;
}
if (ms >= kSecond) {
if (end === -1) {
values.push(`${String(Number((ms / kSecond).toFixed(1)))}s`);
} else {
values.push(`${Math.floor(ms / kSecond)}s`);
if (end === 3) {
end = 1;
}
}
ms = ms % kSecond;
}
if (end !== -1) {
values.push(`${String(Number(ms.toFixed(end)))}ms`);
}

return value.toFixed(3) + unit;
let res = values.pop();
if (values.length) {
res = `${values.pop()} and ${res}`;
}
while (values.length !== 0) {
res = `${values.pop()}, ${res}`;
}
return res;
}

const keyKey = 'Key';
Expand Down
16 changes: 7 additions & 9 deletions test/parallel/test-console-formatTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ require('../common');
const { formatTime } = require('internal/console/constructor');
const assert = require('assert');

const test1 = formatTime(100);
const test2 = formatTime(1500);
const test3 = formatTime(60300);
const test4 = formatTime(4000000);

assert.strictEqual(test1, '100.000ms');
assert.strictEqual(test2, '1.500s');
assert.strictEqual(test3, '1.005min');
assert.strictEqual(test4, '1.111h');
assert.strictEqual(formatTime(100.0096), '100.01ms');
assert.strictEqual(formatTime(100.0115), '100.011ms');
assert.strictEqual(formatTime(1500.04), '1s and 500ms');
assert.strictEqual(formatTime(1500.056), '1s and 500.1ms');
assert.strictEqual(formatTime(60300.3), '1min and 300ms');
assert.strictEqual(formatTime(4000457.4), '1h, 6min and 40.5s');
assert.strictEqual(formatTime(3601017.4), '1h and 1s');