Skip to content

Commit 7eeb794

Browse files
committed
console: fix timeEnd() not coercing the input
The input of console.timeEnd() was not coerced to a string. That way labels were not found and the entry was not removed anymore. PR-URL: nodejs#21779 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent db49589 commit 7eeb794

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/console.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,22 @@ Console.prototype.time = function time(label = 'default') {
236236
};
237237

238238
Console.prototype.timeEnd = function timeEnd(label = 'default') {
239+
// Coerces everything other than Symbol to a string
240+
label = `${label}`;
239241
const hasWarned = timeLogImpl(this, 'timeEnd', label);
240242
if (!hasWarned) {
241243
this._times.delete(label);
242244
}
243245
};
244246

245247
Console.prototype.timeLog = function timeLog(label, ...data) {
248+
// Coerces everything other than Symbol to a string
249+
label = `${label}`;
246250
timeLogImpl(this, 'timeLog', label, data);
247251
};
248252

249253
// Returns true if label was not found
250254
function timeLogImpl(self, name, label = 'default', data) {
251-
// Coerces everything other than Symbol to a string
252-
label = `${label}`;
253255
const time = self._times.get(label);
254256
if (!time) {
255257
process.emitWarning(`No such label '${label}' for console.${name}()`);

test/parallel/test-console.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,14 @@ console.timeEnd('constructor');
135135
console.time('hasOwnProperty');
136136
console.timeEnd('hasOwnProperty');
137137

138-
// verify that values are coerced to strings
138+
// Verify that values are coerced to strings.
139139
console.time([]);
140140
console.timeEnd([]);
141141
console.time({});
142142
console.timeEnd({});
143+
// Repeat the object call to verify that everything really worked.
144+
console.time({});
145+
console.timeEnd({});
143146
console.time(null);
144147
console.timeEnd(null);
145148
console.time(undefined);
@@ -224,6 +227,7 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));
224227
// verify that console.time() coerces label values to strings as expected
225228
assert.ok(/^: \d+\.\d{3}ms$/.test(strings.shift().trim()));
226229
assert.ok(/^\[object Object\]: \d+\.\d{3}ms$/.test(strings.shift().trim()));
230+
assert.ok(/^\[object Object\]: \d+\.\d{3}ms$/.test(strings.shift().trim()));
227231
assert.ok(/^null: \d+\.\d{3}ms$/.test(strings.shift().trim()));
228232
assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim()));
229233
assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim()));

0 commit comments

Comments
 (0)