Skip to content

Commit 10b79d8

Browse files
whatwewantiamkun
authored andcommitted
perf(format): reuse matches instead of created when replacing (#441)
1 parent 1e185b5 commit 10b79d8

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/index.js

+28-26
Original file line numberDiff line numberDiff line change
@@ -272,34 +272,36 @@ class Dayjs {
272272
return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0')
273273
}
274274

275+
const matches = {
276+
YY: String(this.$y).slice(-2),
277+
YYYY: String(this.$y),
278+
M: String(this.$M + 1),
279+
MM: Utils.padStart(this.$M + 1, 2, '0'),
280+
MMM: getShort(locale.monthsShort, this.$M, months, 3),
281+
MMMM: months[this.$M],
282+
D: String(this.$D),
283+
DD: Utils.padStart(this.$D, 2, '0'),
284+
d: String(this.$W),
285+
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
286+
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
287+
dddd: weekdays[this.$W],
288+
H: String(this.$H),
289+
HH: Utils.padStart(this.$H, 2, '0'),
290+
h: get$H('h'),
291+
hh: get$H('hh'),
292+
a: this.$H < 12 ? 'am' : 'pm',
293+
A: this.$H < 12 ? 'AM' : 'PM',
294+
m: String(this.$m),
295+
mm: Utils.padStart(this.$m, 2, '0'),
296+
s: String(this.$s),
297+
ss: Utils.padStart(this.$s, 2, '0'),
298+
SSS: Utils.padStart(this.$ms, 3, '0'),
299+
Z: zoneStr
300+
}
301+
275302
return str.replace(C.REGEX_FORMAT, (match) => {
276303
if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
277-
return {
278-
YY: String(this.$y).slice(-2),
279-
YYYY: String(this.$y),
280-
M: String(this.$M + 1),
281-
MM: Utils.padStart(this.$M + 1, 2, '0'),
282-
MMM: getShort(locale.monthsShort, this.$M, months, 3),
283-
MMMM: months[this.$M],
284-
D: String(this.$D),
285-
DD: Utils.padStart(this.$D, 2, '0'),
286-
d: String(this.$W),
287-
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
288-
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
289-
dddd: weekdays[this.$W],
290-
H: String(this.$H),
291-
HH: Utils.padStart(this.$H, 2, '0'),
292-
h: get$H(match),
293-
hh: get$H(match),
294-
a: this.$H < 12 ? 'am' : 'pm',
295-
A: this.$H < 12 ? 'AM' : 'PM',
296-
m: String(this.$m),
297-
mm: Utils.padStart(this.$m, 2, '0'),
298-
s: String(this.$s),
299-
ss: Utils.padStart(this.$s, 2, '0'),
300-
SSS: Utils.padStart(this.$ms, 3, '0'),
301-
Z: zoneStr
302-
}[match] || zoneStr.replace(':', '') // 'ZZ'
304+
return matches[match] || zoneStr.replace(':', '') // 'ZZ'
303305
})
304306
}
305307

0 commit comments

Comments
 (0)