From 844ef1bdab1f6d4d9dfd9a99571834a1e299aaf0 Mon Sep 17 00:00:00 2001 From: Frederico Silva Date: Sat, 31 Oct 2015 02:13:37 +0100 Subject: [PATCH] console: timeEnd printf-like support --- doc/api/console.markdown | 10 ++++++++-- lib/console.js | 6 +++++- test/parallel/test-console.js | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/api/console.markdown b/doc/api/console.markdown index 6e2de6071e64a2..888b1df620da67 100644 --- a/doc/api/console.markdown +++ b/doc/api/console.markdown @@ -80,11 +80,11 @@ are identified by a unique name. Use the same name when you call output the elapsed time in milliseconds. Timer durations are accurate to the sub-millisecond. -### console.timeEnd(timerName) +### console.timeEnd(timerName, [...]) Stops a timer that was previously started by calling [`console.time()`](#console_console_time_timername) and prints the result to the -console. +console. This function can take multiple arguments in a printf()-like way. Example: @@ -95,6 +95,12 @@ Example: console.timeEnd('100-elements'); // prints 100-elements: 225.438ms + console.time('%d-elements'); + ... + console.timeEnd('%d-elements', 100); + // prints 100-elements: 225.438ms + + ### console.trace(message[, ...]) Print to stderr `'Trace :'`, followed by the formatted message and stack trace diff --git a/lib/console.js b/lib/console.js index d2b4f3fc5b59be..286b077f7513df 100644 --- a/lib/console.js +++ b/lib/console.js @@ -67,7 +67,11 @@ Console.prototype.timeEnd = function(timerName) { } const duration = process.hrtime(time); const ms = duration[0] * 1000 + duration[1] / 1e6; - this.log('%s: %sms', timerName, ms.toFixed(3)); + if (arguments.length === 1) { + this.log('%s: %sms', timerName, ms.toFixed(3)); + } else { + this.log('%s: %sms', util.format.apply(timerName, arguments), ms.toFixed(3)); + } }; diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index f8a927398034cb..c82027365ba0ad 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -46,6 +46,10 @@ console.trace('This is a %j %d', { formatted: 'trace' }, 10, 'foo'); // test console.time() and console.timeEnd() output console.time('label'); console.timeEnd('label'); +console.time('label %s'); +console.timeEnd('label %s', 'hello'); +console.time('label %s %s'); +console.timeEnd('label %s %s', 'hello', 'world'); // verify that Object.prototype properties can be used as labels console.time('__proto__'); @@ -69,6 +73,10 @@ assert.equal(-1, strings.shift().indexOf('baz')); assert.equal('Trace: This is a {"formatted":"trace"} 10 foo', strings.shift().split('\n').shift()); assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim())); +assert.ok(/^label hello: \d+\.\d{3}ms\n/.test(strings.shift()), + 'timeEnd with one extra arg'); +assert.ok(/^label hello world: \d+\.\d{3}ms\n/.test(strings.shift()), + 'timeEnd with more than one extra arg'); assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));