Skip to content

Commit b318f66

Browse files
committed
perf: 避免在循环内 JSON.stringify
1 parent 3e5a814 commit b318f66

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

packages/core/src/context.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ export class PageContext {
419419

420420
const { pages: oldPages, subPackages: oldSubPackages, tabBar: oldTabBar } = cjParse(content || '{}') as CommentObject
421421

422-
const { pages: _, subPackages: __, tabBar: ___, pageJson } = this.pagesGlobConfig || {}
422+
const { pages: _, subPackages: __, tabBar: ___, ...pageJson } = this.pagesGlobConfig || {}
423423

424424
const currentPlatform = platform.toUpperCase()
425425

@@ -435,7 +435,7 @@ export class PageContext {
435435
for (const existing of pageJson.subPackages as unknown as SubPageMetaDatum[]) {
436436
const sub = newSubPackages.get(existing.root)
437437
if (sub) {
438-
existing.pages = mergePlatformItems(existing.pages as any, currentPlatform, sub.pages, 'path') as any
438+
existing.pages = mergePlatformItems(existing.pages, currentPlatform, sub.pages, 'path') as any
439439
newSubPackages.delete(existing.root)
440440
}
441441
}
@@ -510,8 +510,8 @@ function getPagePaths(dir: string, options: ResolvedOptions) {
510510
return pagePaths
511511
}
512512

513-
function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undefined, currentPlatform: string, items: T[], uniqueKeyName: keyof ExcludeIndexSignature<T>): CommentArray<CommentObject> {
514-
const src = source || new CommentArray<CommentObject>()
513+
function mergePlatformItems<T = any>(source: any[] | undefined, currentPlatform: string, items: T[], uniqueKeyName: keyof ExcludeIndexSignature<T>): CommentArray<CommentObject> {
514+
const src = (source as CommentArray<CommentObject>) || new CommentArray<CommentObject>()
515515
currentPlatform = currentPlatform.toUpperCase()
516516

517517
// 1. 从 CommentArray 里抽取第一个注释并获取 platforms 作为 lastPlatforms
@@ -525,14 +525,16 @@ function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undef
525525
}
526526

527527
// 2. 遍历 source,对每个元素进行判断,然后以 uniqueKey 元素的值作为 key 添加到新的 tmpMap 中
528-
const tmpMap = new Map<string, Array<{
529-
item: CommentObject
528+
interface MultiPlatformItem {
529+
item: T
530+
itemStr: string
530531
platforms: string[]
531532
platformStr: string
532-
}>>()
533+
}
534+
const tmpMap = new Map<string, MultiPlatformItem[]>()
533535

534536
for (let i = 0; i < src.length; i++) {
535-
const item = src[i] as CommentObject
537+
const item = src[i] as T
536538
const uniqueKey = (item as any)[uniqueKeyName]
537539

538540
if (!uniqueKey) {
@@ -562,13 +564,13 @@ function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undef
562564
}
563565

564566
const existing = tmpMap.get(uniqueKey) || []
565-
existing.push({ item, platforms, platformStr: platforms.join(' || ') })
567+
existing.push({ item, itemStr: JSON.stringify(item), platforms, platformStr: platforms.join(' || ') })
566568
tmpMap.set(uniqueKey, existing)
567569
}
568570

569571
// 3. 将 items 合并到 tmpMap 中
570572
for (const item of items) {
571-
const newItem = item as any
573+
const newItem = item
572574
const uniqueKey = item[uniqueKeyName] as string
573575

574576
if (!uniqueKey) {
@@ -579,6 +581,7 @@ function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undef
579581
// 如果不存在,则添加到 newMap 中
580582
tmpMap.set(uniqueKey, [{
581583
item: newItem,
584+
itemStr: JSON.stringify(newItem),
582585
platforms: [currentPlatform],
583586
platformStr: currentPlatform,
584587
}])
@@ -589,14 +592,15 @@ function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undef
589592
const existing = tmpMap.get(uniqueKey)!
590593

591594
const newItemStr = JSON.stringify(newItem)
592-
const equalObj = existing.find(val => JSON.stringify(val.item) === newItemStr)
595+
const equalObj = existing.find(val => val.itemStr === newItemStr)
593596
if (equalObj) {
594597
equalObj.platforms.push(currentPlatform)
595598
equalObj.platformStr = equalObj.platforms.join(' || ')
596599
}
597600
else {
598601
existing.push({
599602
item: newItem,
603+
itemStr: newItemStr,
600604
platforms: [currentPlatform],
601605
platformStr: currentPlatform,
602606
})
@@ -632,7 +636,7 @@ function mergePlatformItems<T = any>(source: CommentArray<CommentObject> | undef
632636
// 按照插入顺序处理元素
633637
for (const [_, list] of tmpMap) {
634638
for (const { item, platformStr } of list) {
635-
result.push(item)
639+
result.push(item as CommentObject)
636640

637641
// 检查 platforms 是否和 defaultPlatformStr 一致。(platforms、defaultPlatforms 已预先排序)
638642
if (platformStr !== defaultPlatformStr) {

0 commit comments

Comments
 (0)