Skip to content

Commit b9f0272

Browse files
committed
console: console.time() should not reset a timer when it exists
1 parent 24a5ac7 commit b9f0272

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/console.js

Lines changed: 4 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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)