Open
Description
Describe the bug
- I want to render a page with a big list of some goods with some dates, e.g. expiration
- I have code similar to
const now = dayjs();
const list = [];
for (const item of goods) {
list.push({
item,
expires: now.add(item.daysLeft).format(`D MMMM YYYY`)
});
}
// render the list
This takes too long, because every call to .format()
does much of work under the hood:
Lines 262 to 342 in a9d7d03
The worst things performance-wise:
- the local functions
getShort
,get$H
,meridiemFunc
,matches
are created on every call, even if not needed - the same format string
D MMMM YYYY
is processed viastr.replace(C.REGEX_FORMAT, ...)
again and again on every call
Expected behavior
I'd like to have a way to preprocess/precompile format string in such a way that the following calls to format()
will be very fast. E.g.
const now = dayjs();
const list = [];
const precompiledFormat = dayjs.precompileFormat(`D MMMM YYYY`);
for (const item of goods) {
list.push({
item,
expires: now.add(item.daysLeft).format(precompiledFormat),
// or maybe:
expires: precompiledFormat(now.add(item.daysLeft))
});
}
// render the list
Information
- Day.js Version: 1.9.0
- OS: Mac OS 12
- Browser: Chrome 115
Metadata
Assignees
Labels
No labels