Skip to content

Commit c319c09

Browse files
perf(details): avoid allocating too much formatters
1 parent 516a9f4 commit c319c09

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/routes/[pid=pid]/[org]/[repo]/[id=number]/PageRenderer.svelte

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script module>
1+
<script lang="ts" module>
22
import css from "@shikijs/langs/css";
33
import diff from "@shikijs/langs/diff";
44
import html from "@shikijs/langs/html";
@@ -100,12 +100,10 @@
100100
]
101101
};
102102
103-
function formatToDateTime(date: string) {
104-
return new Intl.DateTimeFormat("en", {
105-
dateStyle: "medium",
106-
timeStyle: "short"
107-
}).format(new Date(date));
108-
}
103+
const dateTimeFormatter = new Intl.DateTimeFormat("en", {
104+
dateStyle: "medium",
105+
timeStyle: "short"
106+
});
109107
110108
type Props = {
111109
metadata: {
@@ -125,7 +123,7 @@
125123
126124
let rightPartInfo = $derived<{ title: string; value: string }[]>([
127125
...("answer_chosen_at" in info && info.answer_chosen_at
128-
? [{ title: "Answered at", value: formatToDateTime(info.answer_chosen_at) }]
126+
? [{ title: "Answered at", value: dateTimeFormatter.format(new Date(info.answer_chosen_at)) }]
129127
: []),
130128
...("answer_chosen_by" in info && info.answer_chosen_by
131129
? [{ title: "Answered by", value: info.answer_chosen_by.login }]
@@ -135,7 +133,7 @@
135133
? [
136134
{
137135
title: "merged" in info && info.merged ? "Merged at" : "Closed at",
138-
value: formatToDateTime(info.closed_at)
136+
value: dateTimeFormatter.format(new Date(info.closed_at))
139137
}
140138
]
141139
: []),
@@ -372,7 +370,7 @@
372370
<span class="hidden xs:block">•</span>
373371
{/if}
374372
{#if "createdAt" in entity}
375-
<span>{formatToDateTime(entity.createdAt)}</span>
373+
<span>{dateTimeFormatter.format(new Date(entity.createdAt))}</span>
376374
{/if}
377375
</div>
378376
</div>
@@ -457,7 +455,7 @@
457455
<span class="mx-1 hidden text-muted-foreground xs:block">•</span>
458456
{/if}
459457
<span class="text-muted-foreground">
460-
{formatToDateTime(info.created_at)}
458+
{dateTimeFormatter.format(new Date(info.created_at))}
461459
</span>
462460
</div>
463461
<!-- Body -->
@@ -612,7 +610,7 @@
612610
<span class="mx-1 hidden text-muted-foreground xs:block">•</span>
613611
{/if}
614612
<span class="text-muted-foreground">
615-
{formatToDateTime(comment.created_at)}
613+
{dateTimeFormatter.format(new Date(comment.created_at))}
616614
</span>
617615
</div>
618616
<!-- Body -->
@@ -654,7 +652,9 @@
654652
<Step icon={GitPullRequestCreateArrow} class="text-base [&>span>svg]:text-green-500">
655653
<div class="flex flex-col">
656654
<span>Pull request open</span>
657-
<span class="text-muted-foreground">{formatToDateTime(info.created_at)}</span>
655+
<span class="text-muted-foreground">
656+
{dateTimeFormatter.format(new Date(info.created_at))}
657+
</span>
658658
</div>
659659
</Step>
660660
{#each commits as commit (commit.sha)}
@@ -695,7 +695,7 @@
695695
</div>
696696
{#if commit.commit.author?.date}
697697
<span class="text-muted-foreground">
698-
• {formatToDateTime(commit.commit.author.date)}
698+
• {dateTimeFormatter.format(new Date(commit.commit.author.date))}
699699
</span>
700700
{/if}
701701
{/if}
@@ -742,7 +742,9 @@
742742
<div class="absolute bottom-0 -left-8 h-4 outline outline-background"></div>
743743
<div class="flex flex-col">
744744
<span>Pull request {info.merged ? "merged" : "closed"}</span>
745-
<span class="text-muted-foreground">{formatToDateTime(info.closed_at)}</span>
745+
<span class="text-muted-foreground">
746+
{dateTimeFormatter.format(new Date(info.closed_at))}
747+
</span>
746748
</div>
747749
</Step>
748750
{/if}

src/routes/package/[...package]/ReleaseCard.svelte

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<script lang="ts" module>
2+
const fullDateFormatter = new Intl.DateTimeFormat("en", {
3+
dateStyle: "long",
4+
timeStyle: "short"
5+
});
6+
</script>
7+
18
<script lang="ts">
29
import { untrack } from "svelte";
310
import { page } from "$app/state";
@@ -295,12 +302,7 @@
295302
class="border bg-popover text-sm text-popover-foreground"
296303
arrowClasses="bg-popover border-b border-r"
297304
>
298-
{isOlderThanAWeek
299-
? timeAgo(releaseDate)
300-
: new Intl.DateTimeFormat("en", {
301-
dateStyle: "long",
302-
timeStyle: "short"
303-
}).format(releaseDate)}
305+
{isOlderThanAWeek ? timeAgo(releaseDate) : fullDateFormatter.format(releaseDate)}
304306
</Tooltip.Content>
305307
</Tooltip.Root>
306308
</Tooltip.Provider>

0 commit comments

Comments
 (0)