@@ -14,6 +14,8 @@ import { getBegin, getEnd } from './shared/dates';
14
14
import { isMaxDate , isMinDate } from './shared/propTypes' ;
15
15
import { between } from './shared/utils' ;
16
16
17
+ const getFormatterOptionsCache = { } ;
18
+
17
19
const defaultMinDate = new Date ( ) ;
18
20
defaultMinDate . setFullYear ( 1 , 0 , 1 ) ;
19
21
defaultMinDate . setHours ( 0 , 0 , 0 , 0 ) ;
@@ -227,16 +229,24 @@ export default class DateInput extends PureComponent {
227
229
get formatDate ( ) {
228
230
const { maxDetail } = this . props ;
229
231
230
- const options = { year : 'numeric' } ;
231
232
const level = allViews . indexOf ( maxDetail ) ;
232
- if ( level >= 2 ) {
233
- options . month = 'numeric' ;
234
- }
235
- if ( level >= 3 ) {
236
- options . day = 'numeric' ;
237
- }
238
-
239
- return getFormatter ( options ) ;
233
+ const formatterOptions =
234
+ getFormatterOptionsCache [ level ] ||
235
+ ( ( ) => {
236
+ const options = { year : 'numeric' } ;
237
+ if ( level >= 2 ) {
238
+ options . month = 'numeric' ;
239
+ }
240
+ if ( level >= 3 ) {
241
+ options . day = 'numeric' ;
242
+ }
243
+
244
+ getFormatterOptionsCache [ level ] = options ;
245
+
246
+ return options ;
247
+ } ) ( ) ;
248
+
249
+ return getFormatter ( formatterOptions ) ;
240
250
}
241
251
242
252
/**
@@ -289,9 +299,17 @@ export default class DateInput extends PureComponent {
289
299
const datePieceReplacements = [ 'y' , 'M' , 'd' ] ;
290
300
291
301
function formatDatePiece ( name , dateToFormat ) {
292
- return getFormatter ( { useGrouping : false , [ name ] : 'numeric' } ) ( locale , dateToFormat ) . match (
293
- / \d { 1 , } / ,
294
- ) ;
302
+ const formatterOptions =
303
+ getFormatterOptionsCache [ name ] ||
304
+ ( ( ) => {
305
+ const options = { useGrouping : false , [ name ] : 'numeric' } ;
306
+
307
+ getFormatterOptionsCache [ name ] = options ;
308
+
309
+ return options ;
310
+ } ) ( ) ;
311
+
312
+ return getFormatter ( formatterOptions ) ( locale , dateToFormat ) . match ( / \d { 1 , } / ) ;
295
313
}
296
314
297
315
let placeholder = formattedDate ;
0 commit comments