@@ -12,9 +12,6 @@ const {
1212 Boolean,
1313 ErrorCaptureStackTrace,
1414 FunctionPrototypeBind,
15- MathFloor,
16- Number,
17- NumberPrototypeToFixed,
1815 ObjectDefineProperties,
1916 ObjectDefineProperty,
2017 ObjectKeys,
@@ -29,10 +26,8 @@ const {
2926 SafeWeakMap,
3027 SafeSet,
3128 StringPrototypeIncludes,
32- StringPrototypePadStart,
3329 StringPrototypeRepeat,
3430 StringPrototypeSlice,
35- StringPrototypeSplit,
3631 Symbol,
3732 SymbolHasInstance,
3833 SymbolToStringTag,
@@ -62,18 +57,13 @@ const {
6257 isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
6358} = require ( 'internal/util/types' ) ;
6459const {
65- CHAR_LOWERCASE_B : kTraceBegin ,
66- CHAR_LOWERCASE_E : kTraceEnd ,
67- CHAR_LOWERCASE_N : kTraceInstant ,
6860 CHAR_UPPERCASE_C : kTraceCount ,
6961} = require ( 'internal/constants' ) ;
7062const kCounts = Symbol ( 'counts' ) ;
63+ const { time, timeLog, timeEnd, kNone } = require ( 'internal/util/debuglog' ) ;
7164
7265const kTraceConsoleCategory = 'node,node.console' ;
7366
74- const kSecond = 1000 ;
75- const kMinute = 60 * kSecond ;
76- const kHour = 60 * kMinute ;
7767const kMaxGroupIndentation = 1000 ;
7868
7969// Lazy loaded for startup performance.
@@ -99,6 +89,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
9989const kBindStreamsLazy = Symbol ( 'kBindStreamsLazy' ) ;
10090const kUseStdout = Symbol ( 'kUseStdout' ) ;
10191const kUseStderr = Symbol ( 'kUseStderr' ) ;
92+ const kInternalTimeLogImpl = Symbol ( 'kInternalTimeLogImpl' ) ;
10293
10394const optionsMap = new SafeWeakMap ( ) ;
10495function Console ( options /* or: stdout, stderr, ignoreErrors = true */ ) {
@@ -373,6 +364,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
373364 } ;
374365}
375366
367+ function timeLogImpl ( label , formatted , args ) {
368+ if ( args === undefined ) {
369+ this . log ( '%s: %s' , label , formatted ) ;
370+ } else {
371+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
372+ }
373+ }
374+
376375const consoleMethods = {
377376 log ( ...args ) {
378377 this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -393,31 +392,21 @@ const consoleMethods = {
393392 } ,
394393
395394 time ( label = 'default' ) {
396- // Coerces everything other than Symbol to a string
397- label = `${ label } ` ;
398- if ( this . _times . has ( label ) ) {
399- process . emitWarning ( `Label '${ label } ' already exists for console.time()` ) ;
400- return ;
401- }
402- trace ( kTraceBegin , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
403- this . _times . set ( label , process . hrtime ( ) ) ;
395+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , kNone , label , `time::${ label } ` ) ;
404396 } ,
405397
406398 timeEnd ( label = 'default' ) {
407- // Coerces everything other than Symbol to a string
408- label = `${ label } ` ;
409- const found = timeLogImpl ( this , 'timeEnd' , label ) ;
410- trace ( kTraceEnd , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
411- if ( found ) {
412- this . _times . delete ( label ) ;
413- }
399+ if ( this [ kInternalTimeLogImpl ] === undefined )
400+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
401+
402+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` ) ;
414403 } ,
415404
416405 timeLog ( label = 'default' , ...data ) {
417- // Coerces everything other than Symbol to a string
418- label = ` ${ label } ` ;
419- timeLogImpl ( this , 'timeLog' , label , data ) ;
420- trace ( kTraceInstant , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
406+ if ( this [ kInternalTimeLogImpl ] === undefined )
407+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
408+
409+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` , data ) ;
421410 } ,
422411
423412 trace : function trace ( ...args ) {
@@ -611,63 +600,6 @@ const consoleMethods = {
611600 } ,
612601} ;
613602
614- // Returns true if label was found
615- function timeLogImpl ( self , name , label , data ) {
616- const time = self . _times . get ( label ) ;
617- if ( time === undefined ) {
618- process . emitWarning ( `No such label '${ label } ' for console.${ name } ()` ) ;
619- return false ;
620- }
621- const duration = process . hrtime ( time ) ;
622- const ms = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
623-
624- const formatted = formatTime ( ms ) ;
625-
626- if ( data === undefined ) {
627- self . log ( '%s: %s' , label , formatted ) ;
628- } else {
629- self . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( data ) ) ;
630- }
631- return true ;
632- }
633-
634- function pad ( value ) {
635- return StringPrototypePadStart ( `${ value } ` , 2 , '0' ) ;
636- }
637-
638- function formatTime ( ms ) {
639- let hours = 0 ;
640- let minutes = 0 ;
641- let seconds = 0 ;
642-
643- if ( ms >= kSecond ) {
644- if ( ms >= kMinute ) {
645- if ( ms >= kHour ) {
646- hours = MathFloor ( ms / kHour ) ;
647- ms = ms % kHour ;
648- }
649- minutes = MathFloor ( ms / kMinute ) ;
650- ms = ms % kMinute ;
651- }
652- seconds = ms / kSecond ;
653- }
654-
655- if ( hours !== 0 || minutes !== 0 ) {
656- ( { 0 : seconds , 1 : ms } = StringPrototypeSplit (
657- NumberPrototypeToFixed ( seconds , 3 ) ,
658- '.' ,
659- ) ) ;
660- const res = hours !== 0 ? `${ hours } :${ pad ( minutes ) } ` : minutes ;
661- return `${ res } :${ pad ( seconds ) } .${ ms } (${ hours !== 0 ? 'h:m' : '' } m:ss.mmm)` ;
662- }
663-
664- if ( seconds !== 0 ) {
665- return `${ NumberPrototypeToFixed ( seconds , 3 ) } s` ;
666- }
667-
668- return `${ Number ( NumberPrototypeToFixed ( ms , 3 ) ) } ms` ;
669- }
670-
671603const keyKey = 'Key' ;
672604const valuesKey = 'Values' ;
673605const indexKey = '(index)' ;
@@ -722,5 +654,4 @@ module.exports = {
722654 kBindStreamsLazy,
723655 kBindProperties,
724656 initializeGlobalConsole,
725- formatTime, // exported for tests
726657} ;
0 commit comments