Skip to content

Commit

Permalink
console: console.time() should not reset a timer when it exists
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
devsnek committed May 3, 2018
1 parent 0930d7e commit a598264
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/console.js
Expand Up @@ -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());
};

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-console.js
Expand Up @@ -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');
Expand Down

0 comments on commit a598264

Please sign in to comment.