perf(finance): 统一把 Intl.DateTimeFormat 提到模块级,消除逐行构造开销
承接 14b595b0。以下 7 处仍在函数体内 `new Intl.DateTimeFormat(...)`, 单次构造约 0.5ms,按行/按条调用时会线性放大: - utils/finance-date-range.ts formatShanghaiDate - utils/http.ts (已在上一个提交修复) - scheduler/cost-finance-sync-job.ts today - finance/sales-facts-service.ts formatDateOnly - finance/order-refund-service.ts toChinaDate - admin/admin-finance-service.ts chinaDateParts - admin/admin-dashboard-service.ts resolveChinaTodayRange - scripts/sync-cost-finance.ts formatShanghaiDate 均为同选项的纯 hoist,输出逐字节不变:formatShanghaiDate 用 9 例跨时区/ 毫秒/闰日语料与旧实现比对一致;其余为相同 en-CA/en-US + Asia/Shanghai 选项的等价替换,formatToParts 的取值改为单次遍历、去掉 fromEntries/reduce。
This commit is contained in:
@@ -526,15 +526,18 @@ function normalizeTimestamp(value: unknown): string {
|
||||
return date.toISOString()
|
||||
}
|
||||
|
||||
/** 模块级只构造一次:`new Intl.DateTimeFormat()` 每次约 0.5ms,逐条调用会线性放大。 */
|
||||
const CHINA_DATE_FORMATTER = new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
})
|
||||
|
||||
function toChinaDate(value: string): string {
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return ''
|
||||
return new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
}).format(date)
|
||||
return CHINA_DATE_FORMATTER.format(date)
|
||||
}
|
||||
|
||||
function formatFen(value: number): string {
|
||||
|
||||
@@ -486,14 +486,17 @@ function mapSalesFact(row: SalesFactRow) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 模块级只构造一次:`new Intl.DateTimeFormat()` 每次约 0.5ms,逐行调用会线性放大。 */
|
||||
const SHANGHAI_DATE_FORMATTER = new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
})
|
||||
|
||||
function formatDateOnly(value: string | Date): string {
|
||||
if (value instanceof Date) {
|
||||
return new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: 'Asia/Shanghai',
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
}).format(value)
|
||||
return SHANGHAI_DATE_FORMATTER.format(value)
|
||||
}
|
||||
return String(value || '').slice(0, 10)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user