From 10b79d890a656d0abdef473fb2dc2b5d29e956a9 Mon Sep 17 00:00:00 2001 From: Eason Date: Sat, 2 Feb 2019 15:56:18 +0800 Subject: [PATCH] perf(format): reuse matches instead of created when replacing (#441) --- src/index.js | 54 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/index.js b/src/index.js index 67f1612ea..cc35dd5f7 100644 --- a/src/index.js +++ b/src/index.js @@ -272,34 +272,36 @@ class Dayjs { return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0') } + const matches = { + YY: String(this.$y).slice(-2), + YYYY: String(this.$y), + M: String(this.$M + 1), + MM: Utils.padStart(this.$M + 1, 2, '0'), + MMM: getShort(locale.monthsShort, this.$M, months, 3), + MMMM: months[this.$M], + D: String(this.$D), + DD: Utils.padStart(this.$D, 2, '0'), + d: String(this.$W), + dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2), + ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3), + dddd: weekdays[this.$W], + H: String(this.$H), + HH: Utils.padStart(this.$H, 2, '0'), + h: get$H('h'), + hh: get$H('hh'), + a: this.$H < 12 ? 'am' : 'pm', + A: this.$H < 12 ? 'AM' : 'PM', + m: String(this.$m), + mm: Utils.padStart(this.$m, 2, '0'), + s: String(this.$s), + ss: Utils.padStart(this.$s, 2, '0'), + SSS: Utils.padStart(this.$ms, 3, '0'), + Z: zoneStr + } + return str.replace(C.REGEX_FORMAT, (match) => { if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '') - return { - YY: String(this.$y).slice(-2), - YYYY: String(this.$y), - M: String(this.$M + 1), - MM: Utils.padStart(this.$M + 1, 2, '0'), - MMM: getShort(locale.monthsShort, this.$M, months, 3), - MMMM: months[this.$M], - D: String(this.$D), - DD: Utils.padStart(this.$D, 2, '0'), - d: String(this.$W), - dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2), - ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3), - dddd: weekdays[this.$W], - H: String(this.$H), - HH: Utils.padStart(this.$H, 2, '0'), - h: get$H(match), - hh: get$H(match), - a: this.$H < 12 ? 'am' : 'pm', - A: this.$H < 12 ? 'AM' : 'PM', - m: String(this.$m), - mm: Utils.padStart(this.$m, 2, '0'), - s: String(this.$s), - ss: Utils.padStart(this.$s, 2, '0'), - SSS: Utils.padStart(this.$ms, 3, '0'), - Z: zoneStr - }[match] || zoneStr.replace(':', '') // 'ZZ' + return matches[match] || zoneStr.replace(':', '') // 'ZZ' }) }