Skip to content

Commit b016745

Browse files
committed
console: implement timeLog method
Refs: whatwg/console#138 PR-URL: #21312 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7951e6d commit b016745

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

doc/api/console.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,25 @@ console.timeEnd('100-elements');
413413
// prints 100-elements: 225.438ms
414414
```
415415

416+
### console.timeLog([label][, ...data])
417+
<!-- YAML
418+
added: REPLACEME
419+
-->
420+
* `label` {string} **Default:** `'default'`
421+
* `...data` {any}
422+
423+
For a timer that was previously started by calling [`console.time()`][], prints
424+
the elapsed time and other `data` arguments to `stdout`:
425+
426+
```js
427+
console.time('process');
428+
const value = expensiveProcess1(); // Returns 42
429+
console.timeLog('process', value);
430+
// Prints "process: 365.227ms 42".
431+
doExpensiveProcess2(value);
432+
console.timeEnd('process');
433+
```
434+
416435
### console.trace([message][, ...args])
417436
<!-- YAML
418437
added: v0.1.104

lib/console.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,34 @@ Console.prototype.time = function time(label = 'default') {
235235
};
236236

237237
Console.prototype.timeEnd = function timeEnd(label = 'default') {
238+
const hasWarned = timeLogImpl(this, 'timeEnd', label);
239+
if (!hasWarned) {
240+
this._times.delete(label);
241+
}
242+
};
243+
244+
Console.prototype.timeLog = function timeLog(label, ...data) {
245+
timeLogImpl(this, 'timeLog', label, data);
246+
};
247+
248+
// Returns true if label was not found
249+
function timeLogImpl(self, name, label = 'default', data) {
238250
// Coerces everything other than Symbol to a string
239251
label = `${label}`;
240-
const time = this._times.get(label);
252+
const time = self._times.get(label);
241253
if (!time) {
242-
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
243-
return;
254+
process.emitWarning(`No such label '${label}' for console.${name}()`);
255+
return true;
244256
}
245257
const duration = process.hrtime(time);
246258
const ms = duration[0] * 1000 + duration[1] / 1e6;
247-
this.log('%s: %sms', label, ms.toFixed(3));
248-
this._times.delete(label);
249-
};
259+
if (data === undefined) {
260+
self.log('%s: %sms', label, ms.toFixed(3));
261+
} else {
262+
self.log('%s: %sms', label, ms.toFixed(3), ...data);
263+
}
264+
return false;
265+
}
250266

251267
Console.prototype.trace = function trace(...args) {
252268
const err = {

test/parallel/test-console.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ if (common.isMainThread) {
3131
assert.strictEqual(typeof process.stdout.fd, 'number');
3232
assert.strictEqual(typeof process.stderr.fd, 'number');
3333
}
34-
process.once('warning', common.mustCall((warning) => {
35-
assert(/no such label/.test(warning.message));
36-
}));
3734

38-
console.timeEnd('no such label');
35+
common.expectWarning(
36+
'Warning',
37+
[
38+
['No such label \'nolabel\' for console.timeEnd()', common.noWarnCode],
39+
['No such label \'nolabel\' for console.timeLog()', common.noWarnCode],
40+
['Label \'test\' already exists for console.time()', common.noWarnCode]
41+
]
42+
);
43+
44+
console.timeEnd('nolabel');
45+
console.timeLog('nolabel');
3946

4047
console.time('label');
4148
console.timeEnd('label');
@@ -144,15 +151,16 @@ console.timeEnd(NaN);
144151
console.time('test');
145152
const time = console._times.get('test');
146153
setTimeout(() => {
147-
common.expectWarning(
148-
'Warning',
149-
'Label \'test\' already exists for console.time()',
150-
common.noWarnCode);
151154
console.time('test');
152155
assert.deepStrictEqual(console._times.get('test'), time);
153156
console.timeEnd('test');
154157
}, 1);
155158

159+
console.time('log1');
160+
console.timeLog('log1');
161+
console.timeLog('log1', 'test');
162+
console.timeLog('log1', {}, [1, 2, 3]);
163+
console.timeEnd('log1');
156164

157165
console.assert(false, '%s should', 'console.assert', 'not throw');
158166
assert.strictEqual(errStrings[errStrings.length - 1],
@@ -219,6 +227,14 @@ assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim()));
219227
assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim()));
220228
assert.ok(/^NaN: \d+\.\d{3}ms$/.test(strings.shift().trim()));
221229

230+
assert.ok(/^log1: \d+\.\d{3}ms$/.test(strings.shift().trim()));
231+
assert.ok(/^log1: \d+\.\d{3}ms test$/.test(strings.shift().trim()));
232+
assert.ok(/^log1: \d+\.\d{3}ms {} \[ 1, 2, 3 ]$/.test(strings.shift().trim()));
233+
assert.ok(/^log1: \d+\.\d{3}ms$/.test(strings.shift().trim()));
234+
235+
// Make sure that we checked all strings
236+
assert.strictEqual(strings.length, 0);
237+
222238
assert.strictEqual(errStrings.shift().split('\n').shift(),
223239
'Trace: This is a {"formatted":"trace"} 10 foo');
224240

@@ -229,6 +245,6 @@ common.hijackStderr(common.mustCall(function(data) {
229245

230246
// stderr.write will catch sync error, so use `process.nextTick` here
231247
process.nextTick(function() {
232-
assert.strictEqual(data.includes('no such label'), true);
248+
assert.strictEqual(data.includes('nolabel'), true);
233249
});
234250
}));

0 commit comments

Comments
 (0)