Skip to content

Commit 480b590

Browse files
committed
Fix memory leak when calling getFormatter
Closes #405
1 parent 1624ecf commit 480b590

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/DateInput.jsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { getBegin, getEnd } from './shared/dates';
1414
import { isMaxDate, isMinDate } from './shared/propTypes';
1515
import { between } from './shared/utils';
1616

17+
const getFormatterOptionsCache = {};
18+
1719
const defaultMinDate = new Date();
1820
defaultMinDate.setFullYear(1, 0, 1);
1921
defaultMinDate.setHours(0, 0, 0, 0);
@@ -227,16 +229,24 @@ export default class DateInput extends PureComponent {
227229
get formatDate() {
228230
const { maxDetail } = this.props;
229231

230-
const options = { year: 'numeric' };
231232
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);
240250
}
241251

242252
/**
@@ -289,9 +299,17 @@ export default class DateInput extends PureComponent {
289299
const datePieceReplacements = ['y', 'M', 'd'];
290300

291301
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,}/);
295313
}
296314

297315
let placeholder = formattedDate;

0 commit comments

Comments
 (0)