Skip to content

Commit 4da8b99

Browse files
committed
console: coerce label to string in console.time()
Per the console spec, the label in console.time() is a string. Per the console spec, the default value of label is `'default'`. PR-URL: #14643 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 80ebb42 commit 4da8b99

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

doc/api/console.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ See [`util.format()`][] for more information.
321321
<!-- YAML
322322
added: v0.1.104
323323
-->
324-
* `label` {string}
324+
* `label` {string} Defaults to `'default'`.
325325

326326
Starts a timer that can be used to compute the duration of an operation. Timers
327327
are identified by a unique `label`. Use the same `label` when calling
@@ -337,7 +337,7 @@ changes:
337337
description: This method no longer supports multiple calls that don’t map
338338
to individual `console.time()` calls; see below for details.
339339
-->
340-
* `label` {string}
340+
* `label` {string} Defaults to `'default'`.
341341

342342
Stops a timer that was previously started by calling [`console.time()`][] and
343343
prints the result to `stdout`:

lib/console.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ Console.prototype.dir = function dir(object, options) {
139139
};
140140

141141

142-
Console.prototype.time = function time(label) {
142+
Console.prototype.time = function time(label = 'default') {
143+
// Coerces everything other than Symbol to a string
144+
label = `${label}`;
143145
this._times.set(label, process.hrtime());
144146
};
145147

146148

147-
Console.prototype.timeEnd = function timeEnd(label) {
149+
Console.prototype.timeEnd = function timeEnd(label = 'default') {
150+
// Coerces everything other than Symbol to a string
151+
label = `${label}`;
148152
const time = this._times.get(label);
149153
if (!time) {
150154
process.emitWarning(`No such label '${label}' for console.timeEnd()`);

test/parallel/test-console.js

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ assert.doesNotThrow(function() {
4242
console.timeEnd('label');
4343
});
4444

45+
assert.throws(() => console.time(Symbol('test')),
46+
/^TypeError: Cannot convert a Symbol value to a string$/);
47+
assert.throws(() => console.timeEnd(Symbol('test')),
48+
/^TypeError: Cannot convert a Symbol value to a string$/);
49+
50+
4551
// an Object with a custom .inspect() function
4652
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' };
4753

@@ -103,6 +109,20 @@ console.timeEnd('constructor');
103109
console.time('hasOwnProperty');
104110
console.timeEnd('hasOwnProperty');
105111

112+
// verify that values are coerced to strings
113+
console.time([]);
114+
console.timeEnd([]);
115+
console.time({});
116+
console.timeEnd({});
117+
console.time(null);
118+
console.timeEnd(null);
119+
console.time(undefined);
120+
console.timeEnd('default');
121+
console.time('default');
122+
console.timeEnd();
123+
console.time(NaN);
124+
console.timeEnd(NaN);
125+
106126
assert.strictEqual(strings.length, process.stdout.writeTimes);
107127
assert.strictEqual(errStrings.length, process.stderr.writeTimes);
108128
common.restoreStdout();

0 commit comments

Comments
 (0)