Skip to content

Commit

Permalink
console: coerce label to string in console.time()
Browse files Browse the repository at this point in the history
Per the console spec, the label in console.time() is a string.
Per the console spec, the default value of label is `'default'`.

PR-URL: #14643
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
jasnell committed Aug 8, 2017
1 parent 80ebb42 commit 4da8b99
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ See [`util.format()`][] for more information.
<!-- YAML
added: v0.1.104
-->
* `label` {string}
* `label` {string} Defaults to `'default'`.

Starts a timer that can be used to compute the duration of an operation. Timers
are identified by a unique `label`. Use the same `label` when calling
Expand All @@ -337,7 +337,7 @@ changes:
description: This method no longer supports multiple calls that don’t map
to individual `console.time()` calls; see below for details.
-->
* `label` {string}
* `label` {string} Defaults to `'default'`.

Stops a timer that was previously started by calling [`console.time()`][] and
prints the result to `stdout`:
Expand Down
8 changes: 6 additions & 2 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ Console.prototype.dir = function dir(object, options) {
};


Console.prototype.time = function time(label) {
Console.prototype.time = function time(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
this._times.set(label, process.hrtime());
};


Console.prototype.timeEnd = function timeEnd(label) {
Console.prototype.timeEnd = function timeEnd(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
const time = this._times.get(label);
if (!time) {
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ assert.doesNotThrow(function() {
console.timeEnd('label');
});

assert.throws(() => console.time(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);
assert.throws(() => console.timeEnd(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);


// an Object with a custom .inspect() function
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' };

Expand Down Expand Up @@ -103,6 +109,20 @@ console.timeEnd('constructor');
console.time('hasOwnProperty');
console.timeEnd('hasOwnProperty');

// verify that values are coerced to strings
console.time([]);
console.timeEnd([]);
console.time({});
console.timeEnd({});
console.time(null);
console.timeEnd(null);
console.time(undefined);
console.timeEnd('default');
console.time('default');
console.timeEnd();
console.time(NaN);
console.timeEnd(NaN);

assert.strictEqual(strings.length, process.stdout.writeTimes);
assert.strictEqual(errStrings.length, process.stderr.writeTimes);
common.restoreStdout();
Expand Down

0 comments on commit 4da8b99

Please sign in to comment.