@@ -13,9 +13,6 @@ const {
1313 Boolean,
1414 ErrorCaptureStackTrace,
1515 FunctionPrototypeBind,
16- MathFloor,
17- Number,
18- NumberPrototypeToFixed,
1916 ObjectDefineProperties,
2017 ObjectDefineProperty,
2118 ObjectKeys,
@@ -30,10 +27,8 @@ const {
3027 SafeSet,
3128 SafeWeakMap,
3229 StringPrototypeIncludes,
33- StringPrototypePadStart,
3430 StringPrototypeRepeat,
3531 StringPrototypeSlice,
36- StringPrototypeSplit,
3732 Symbol,
3833 SymbolHasInstance,
3934 SymbolToStringTag,
@@ -63,19 +58,14 @@ const {
6358 isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
6459} = require ( 'internal/util/types' ) ;
6560const {
66- CHAR_LOWERCASE_B : kTraceBegin ,
67- CHAR_LOWERCASE_E : kTraceEnd ,
68- CHAR_LOWERCASE_N : kTraceInstant ,
6961 CHAR_UPPERCASE_C : kTraceCount ,
7062} = require ( 'internal/constants' ) ;
7163const { styleText } = require ( 'util' ) ;
7264const kCounts = Symbol ( 'counts' ) ;
65+ const { time, timeLog, timeEnd, kNone } = require ( 'internal/util/debuglog' ) ;
7366
7467const kTraceConsoleCategory = 'node,node.console' ;
7568
76- const kSecond = 1000 ;
77- const kMinute = 60 * kSecond ;
78- const kHour = 60 * kMinute ;
7969const kMaxGroupIndentation = 1000 ;
8070
8171// Lazy loaded for startup performance.
@@ -101,6 +91,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
10191const kBindStreamsLazy = Symbol ( 'kBindStreamsLazy' ) ;
10292const kUseStdout = Symbol ( 'kUseStdout' ) ;
10393const kUseStderr = Symbol ( 'kUseStderr' ) ;
94+ const kInternalTimeLogImpl = Symbol ( 'kInternalTimeLogImpl' ) ;
10495
10596const optionsMap = new SafeWeakMap ( ) ;
10697function Console ( options /* or: stdout, stderr, ignoreErrors = true */ ) {
@@ -381,6 +372,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
381372 } ;
382373}
383374
375+ function timeLogImpl ( label , formatted , args ) {
376+ if ( args === undefined ) {
377+ this . log ( '%s: %s' , label , formatted ) ;
378+ } else {
379+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
380+ }
381+ }
382+
384383const consoleMethods = {
385384 log ( ...args ) {
386385 this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -404,31 +403,21 @@ const consoleMethods = {
404403 } ,
405404
406405 time ( label = 'default' ) {
407- // Coerces everything other than Symbol to a string
408- label = `${ label } ` ;
409- if ( this . _times . has ( label ) ) {
410- process . emitWarning ( `Label '${ label } ' already exists for console.time()` ) ;
411- return ;
412- }
413- trace ( kTraceBegin , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
414- this . _times . set ( label , process . hrtime ( ) ) ;
406+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , kNone , label , `time::${ label } ` ) ;
415407 } ,
416408
417409 timeEnd ( label = 'default' ) {
418- // Coerces everything other than Symbol to a string
419- label = `${ label } ` ;
420- const found = timeLogImpl ( this , 'timeEnd' , label ) ;
421- trace ( kTraceEnd , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
422- if ( found ) {
423- this . _times . delete ( label ) ;
424- }
410+ if ( this [ kInternalTimeLogImpl ] === undefined )
411+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
412+
413+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` ) ;
425414 } ,
426415
427416 timeLog ( label = 'default' , ...data ) {
428- // Coerces everything other than Symbol to a string
429- label = ` ${ label } ` ;
430- timeLogImpl ( this , 'timeLog' , label , data ) ;
431- trace ( kTraceInstant , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
417+ if ( this [ kInternalTimeLogImpl ] === undefined )
418+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
419+
420+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` , data ) ;
432421 } ,
433422
434423 trace : function trace ( ...args ) {
@@ -627,63 +616,6 @@ const consoleMethods = {
627616 } ,
628617} ;
629618
630- // Returns true if label was found
631- function timeLogImpl ( self , name , label , data ) {
632- const time = self . _times . get ( label ) ;
633- if ( time === undefined ) {
634- process . emitWarning ( `No such label '${ label } ' for console.${ name } ()` ) ;
635- return false ;
636- }
637- const duration = process . hrtime ( time ) ;
638- const ms = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
639-
640- const formatted = formatTime ( ms ) ;
641-
642- if ( data === undefined ) {
643- self . log ( '%s: %s' , label , formatted ) ;
644- } else {
645- self . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( data ) ) ;
646- }
647- return true ;
648- }
649-
650- function pad ( value ) {
651- return StringPrototypePadStart ( `${ value } ` , 2 , '0' ) ;
652- }
653-
654- function formatTime ( ms ) {
655- let hours = 0 ;
656- let minutes = 0 ;
657- let seconds = 0 ;
658-
659- if ( ms >= kSecond ) {
660- if ( ms >= kMinute ) {
661- if ( ms >= kHour ) {
662- hours = MathFloor ( ms / kHour ) ;
663- ms = ms % kHour ;
664- }
665- minutes = MathFloor ( ms / kMinute ) ;
666- ms = ms % kMinute ;
667- }
668- seconds = ms / kSecond ;
669- }
670-
671- if ( hours !== 0 || minutes !== 0 ) {
672- ( { 0 : seconds , 1 : ms } = StringPrototypeSplit (
673- NumberPrototypeToFixed ( seconds , 3 ) ,
674- '.' ,
675- ) ) ;
676- const res = hours !== 0 ? `${ hours } :${ pad ( minutes ) } ` : minutes ;
677- return `${ res } :${ pad ( seconds ) } .${ ms } (${ hours !== 0 ? 'h:m' : '' } m:ss.mmm)` ;
678- }
679-
680- if ( seconds !== 0 ) {
681- return `${ NumberPrototypeToFixed ( seconds , 3 ) } s` ;
682- }
683-
684- return `${ Number ( NumberPrototypeToFixed ( ms , 3 ) ) } ms` ;
685- }
686-
687619const keyKey = 'Key' ;
688620const valuesKey = 'Values' ;
689621const indexKey = '(index)' ;
@@ -743,5 +675,4 @@ module.exports = {
743675 kBindStreamsLazy,
744676 kBindProperties,
745677 initializeGlobalConsole,
746- formatTime, // exported for tests
747678} ;
0 commit comments