Skip to content

Commit a598264

Browse files
committed
console: console.time() should not reset a timer when it exists
PR-URL: #20442 Fixes: #20440 Refs: https://console.spec.whatwg.org/#time Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
1 parent 0930d7e commit a598264

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/console.js

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ Console.prototype.dir = function dir(object, options) {
225225
Console.prototype.time = function time(label = 'default') {
226226
// Coerces everything other than Symbol to a string
227227
label = `${label}`;
228+
if (this._times.has(label)) {
229+
process.emitWarning(`Label '${label}' already exists for console.time()`);
230+
return;
231+
}
228232
this._times.set(label, process.hrtime());
229233
};
230234

test/parallel/test-console.js

+14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ console.timeEnd();
138138
console.time(NaN);
139139
console.timeEnd(NaN);
140140

141+
// make sure calling time twice without timeEnd doesn't reset the timer.
142+
console.time('test');
143+
const time = console._times.get('test');
144+
setTimeout(() => {
145+
assert.deepStrictEqual(console._times.get('test'), time);
146+
common.expectWarning(
147+
'Warning',
148+
'Label \'test\' already exists for console.time()',
149+
common.noWarnCode);
150+
console.time('test');
151+
console.timeEnd('test');
152+
}, 1);
153+
154+
141155
console.assert(false, '%s should', 'console.assert', 'not throw');
142156
assert.strictEqual(errStrings[errStrings.length - 1],
143157
'Assertion failed: console.assert should not throw\n');

0 commit comments

Comments
 (0)