Skip to content

Commit 7026588

Browse files
committed
Implement formatters caching to improve performance
1 parent 26083eb commit 7026588

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/date.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ var getIntlFormatter = function(format) {
2222
};
2323
};
2424

25+
var formattersCache = {};
26+
var getFormatter = function(format) {
27+
var key = locale() + '/' + JSON.stringify(format);
28+
if(!formattersCache[key]) {
29+
formattersCache[key] = (new Intl.DateTimeFormat(locale(), format)).format;
30+
}
31+
32+
return formattersCache[key];
33+
};
34+
2535
var formatDateTime = function(date, format) {
26-
return (new Intl.DateTimeFormat(locale(), format)).format(date).replace(SYMBOLS_TO_REMOVE_REGEX, '');
36+
return getFormatter(format)(date).replace(SYMBOLS_TO_REMOVE_REGEX, '');
2737
};
2838

2939
var formatNumber = function(number) {

src/number.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ var compareVersions = require('devextreme/core/utils/version').compare;
77

88
var currencyOptionsCache = {},
99
detectCurrencySymbolRegex = /([^\s0]+)?(\s*)0*[.,]*0*(\s*)([^\s0]+)?/,
10+
formattersCache = {},
1011
getFormatter = function(format) {
11-
return (new Intl.NumberFormat(locale(), format)).format;
12+
var key = locale() + '/' + JSON.stringify(format);
13+
if(!formattersCache[key]) {
14+
formattersCache[key] = (new Intl.NumberFormat(locale(), format)).format;
15+
}
16+
17+
return formattersCache[key];
1218
},
1319
getCurrencyFormatter = function(currency) {
1420
return (new Intl.NumberFormat(locale(), { style: 'currency', currency: currency }));

0 commit comments

Comments
 (0)