diff --git a/lib/console.js b/lib/console.js index a0158ec6643782..07f177d595deb2 100644 --- a/lib/console.js +++ b/lib/console.js @@ -225,6 +225,10 @@ Console.prototype.dir = function dir(object, options) { Console.prototype.time = function time(label = 'default') { // Coerces everything other than Symbol to a string label = `${label}`; + if (this._times.has(label)) { + process.emitWarning(`Label '${label}' already exists for console.time()`); + return; + } this._times.set(label, process.hrtime()); }; diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 4bac35c65dfc70..64df389be691c6 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -138,6 +138,20 @@ console.timeEnd(); console.time(NaN); console.timeEnd(NaN); +// make sure calling time twice without timeEnd doesn't reset the timer. +console.time('test'); +const time = console._times.get('test'); +setTimeout(() => { + assert.deepStrictEqual(console._times.get('test'), time); + common.expectWarning( + 'Warning', + 'Label \'test\' already exists for console.time()', + common.noWarnCode); + console.time('test'); + console.timeEnd('test'); +}, 1); + + console.assert(false, '%s should', 'console.assert', 'not throw'); assert.strictEqual(errStrings[errStrings.length - 1], 'Assertion failed: console.assert should not throw\n');