@@ -28,15 +28,134 @@ type DateFormatOptions = {
2828const DateFormatWrapped = DateFormatNative as typeof DateFormatT ;
2929
3030class DateFormat extends DateFormatWrapped {
31+ /**
32+ * Central cache for all DateFormat instances across the entire library.
33+ * Shared by Calendar, DatePicker, TimePicker, and all other date components.
34+ * Key format: "type:JSON.stringify(options):locale"
35+ * @private
36+ */
37+ private static _cache = new Map < string , DateFormat > ( ) ;
38+
39+ private static _stats = {
40+ totalCalls : 0 ,
41+ cacheHits : 0 ,
42+ cacheMisses : 0 ,
43+ uniqueInstances : 0 ,
44+ } ;
45+
46+ static logCacheStats ( ) {
47+ console . log ( "===== DateFormat Cache Statistics =====" ) ;
48+ console . log ( `Total calls: ${ DateFormat . _stats . totalCalls } ` ) ;
49+ console . log ( `Cache hits: ${ DateFormat . _stats . cacheHits } (${ DateFormat . _stats . totalCalls > 0 ? ( ( DateFormat . _stats . cacheHits / DateFormat . _stats . totalCalls ) * 100 ) . toFixed ( 1 ) : 0 } %)` ) ;
50+ console . log ( `Cache misses: ${ DateFormat . _stats . cacheMisses } (${ DateFormat . _stats . totalCalls > 0 ? ( ( DateFormat . _stats . cacheMisses / DateFormat . _stats . totalCalls ) * 100 ) . toFixed ( 1 ) : 0 } %)` ) ;
51+ console . log ( `Unique instances: ${ DateFormat . _cache . size } ` ) ;
52+ console . log ( `Total instances: ${ ( window as any ) . dateformatinstances ?. size || 0 } ` ) ;
53+ console . log ( "Cache keys:" ) ;
54+ Array . from ( DateFormat . _cache . keys ( ) ) . forEach ( ( key , index ) => {
55+ console . log ( ` ${ index + 1 } . ${ key } ` ) ;
56+ } ) ;
57+ console . log ( "=========================================\n" ) ;
58+
59+ return {
60+ totalCalls : DateFormat . _stats . totalCalls ,
61+ cacheHits : DateFormat . _stats . cacheHits ,
62+ cacheMisses : DateFormat . _stats . cacheMisses ,
63+ uniqueInstances : DateFormat . _cache . size ,
64+ totalInstances : ( window as any ) . dateformatinstances ?. size || 0 ,
65+ } ;
66+ }
67+
3168 static getDateInstance ( oFormatOptions ?: DateFormatOptions , oLocale ?: LocaleWrapped ) : DateFormat ;
3269 static getDateInstance ( oLocale ?: LocaleWrapped ) : DateFormat ;
3370 static getDateInstance ( oFormatOptionsOrLocale ?: DateFormatOptions | LocaleWrapped , oLocale ?: LocaleWrapped ) : DateFormat {
71+ DateFormat . _stats . totalCalls ++ ;
72+
73+ if ( oFormatOptionsOrLocale instanceof LocaleWrapped ) {
74+ return DateFormatWrapped . getDateInstance ( undefined , oFormatOptionsOrLocale ) ;
75+ }
76+
77+ const nativeLocale = oLocale ?? new LocaleWrapped ( getLocale ( ) . toString ( ) ) ;
78+ const cacheKey = `date:${ JSON . stringify ( oFormatOptionsOrLocale || { } ) } :${ nativeLocale . toString ( ) } ` ;
79+
80+ if ( ! DateFormat . _cache . has ( cacheKey ) ) {
81+ DateFormat . _stats . cacheMisses ++ ;
82+ console . log ( `[DateFormat CACHE MISS #${ DateFormat . _stats . cacheMisses } ] Creating NEW date instance | Total instances: ${ DateFormat . _cache . size + 1 } ` ) ;
83+ console . log ( ` Key: ${ cacheKey } ` ) ;
84+ const date = DateFormatWrapped . getDateInstance ( oFormatOptionsOrLocale , nativeLocale ) ;
85+ DateFormat . _cache . set ( cacheKey , date ) ;
86+ } else {
87+ DateFormat . _stats . cacheHits ++ ;
88+ console . log ( `[DateFormat CACHE HIT #${ DateFormat . _stats . cacheHits } ] Reusing CACHED date instance` ) ;
89+ }
90+
91+ return DateFormat . _cache . get ( cacheKey ) ! ;
92+ }
93+
94+ static getTimeInstance ( oFormatOptions ?: DateFormatOptions , oLocale ?: LocaleWrapped ) : DateFormat ;
95+ static getTimeInstance ( oLocale ?: LocaleWrapped ) : DateFormat ;
96+ static getTimeInstance ( oFormatOptionsOrLocale ?: DateFormatOptions | LocaleWrapped , oLocale ?: LocaleWrapped ) : DateFormat {
97+ DateFormat . _stats . totalCalls ++ ;
98+
3499 if ( oFormatOptionsOrLocale instanceof LocaleWrapped ) {
35- return DateFormatWrapped . getDateInstance ( undefined , oFormatOptionsOrLocale ) ;
100+ return DateFormatWrapped . getTimeInstance ( undefined , oFormatOptionsOrLocale ) ;
36101 }
102+
37103 const nativeLocale = oLocale ?? new LocaleWrapped ( getLocale ( ) . toString ( ) ) ;
38- return DateFormatWrapped . getDateInstance ( oFormatOptionsOrLocale , nativeLocale ) ;
104+ const cacheKey = `time:${ JSON . stringify ( oFormatOptionsOrLocale || { } ) } :${ nativeLocale . toString ( ) } ` ;
105+
106+ if ( ! DateFormat . _cache . has ( cacheKey ) ) {
107+ DateFormat . _stats . cacheMisses ++ ;
108+ console . log ( `[DateFormat CACHE MISS #${ DateFormat . _stats . cacheMisses } ] Creating NEW time instance | Total instances: ${ DateFormat . _cache . size + 1 } ` ) ;
109+ console . log ( ` Key: ${ cacheKey } ` ) ;
110+ const time = DateFormatWrapped . getTimeInstance ( oFormatOptionsOrLocale , nativeLocale ) ;
111+ DateFormat . _cache . set ( cacheKey , time ) ;
112+ } else {
113+ DateFormat . _stats . cacheHits ++ ;
114+ console . log ( `[DateFormat CACHE HIT #${ DateFormat . _stats . cacheHits } ] Reusing CACHED time instance` ) ;
115+ }
116+
117+ return DateFormat . _cache . get ( cacheKey ) ! ;
39118 }
119+
120+ static getDateTimeInstance ( oFormatOptions ?: DateFormatOptions , oLocale ?: LocaleWrapped ) : DateFormat ;
121+ static getDateTimeInstance ( oLocale ?: LocaleWrapped ) : DateFormat ;
122+ static getDateTimeInstance ( oFormatOptionsOrLocale ?: DateFormatOptions | LocaleWrapped , oLocale ?: LocaleWrapped ) : DateFormat {
123+ DateFormat . _stats . totalCalls ++ ;
124+
125+ if ( oFormatOptionsOrLocale instanceof LocaleWrapped ) {
126+ return DateFormatWrapped . getDateTimeInstance ( undefined , oFormatOptionsOrLocale ) ;
127+ }
128+
129+ const nativeLocale = oLocale ?? new LocaleWrapped ( getLocale ( ) . toString ( ) ) ;
130+ const cacheKey = `datetime:${ JSON . stringify ( oFormatOptionsOrLocale || { } ) } :${ nativeLocale . toString ( ) } ` ;
131+
132+ if ( ! DateFormat . _cache . has ( cacheKey ) ) {
133+ DateFormat . _stats . cacheMisses ++ ;
134+ console . log ( `[DateFormat CACHE MISS #${ DateFormat . _stats . cacheMisses } ] Creating NEW datetime instance | Total instances: ${ DateFormat . _cache . size + 1 } ` ) ;
135+ console . log ( ` Key: ${ cacheKey } ` ) ;
136+ const datetime = DateFormatWrapped . getDateTimeInstance ( oFormatOptionsOrLocale , nativeLocale ) ;
137+ DateFormat . _cache . set ( cacheKey , datetime ) ;
138+ } else {
139+ DateFormat . _stats . cacheHits ++ ;
140+ console . log ( `[DateFormat CACHE HIT #${ DateFormat . _stats . cacheHits } ] Reusing CACHED datetime instance` ) ;
141+ }
142+
143+ return DateFormat . _cache . get ( cacheKey ) ! ;
144+ }
145+ }
146+
147+ // @ts -ignore
148+ if ( typeof window !== "undefined" ) {
149+ // @ts -ignore
150+ window . DateFormatStats = {
151+ log : ( ) => DateFormat . logCacheStats ( ) ,
152+ get : ( ) => ( {
153+ totalCalls : DateFormat [ "_stats" ] . totalCalls ,
154+ cacheHits : DateFormat [ "_stats" ] . cacheHits ,
155+ cacheMisses : DateFormat [ "_stats" ] . cacheMisses ,
156+ uniqueInstances : DateFormat [ "_cache" ] . size ,
157+ } ) ,
158+ } ;
40159}
41160
42161export default DateFormat ;
0 commit comments