Skip to content

Commit 327f156

Browse files
authored
perf: avoid creating Date objects when parsing timestamps (#2256)
1 parent 13cf135 commit 327f156

File tree

9 files changed

+11
-11
lines changed

9 files changed

+11
-11
lines changed

app/composables/npm/usePackage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function transformPackument(
3737
const timeA = pkg.time[a]
3838
const timeB = pkg.time[b]
3939
if (!timeA || !timeB) return 0
40-
return new Date(timeB).getTime() - new Date(timeA).getTime()
40+
return Date.parse(timeB) - Date.parse(timeA)
4141
})
4242
.slice(0, RECENT_VERSIONS_COUNT)
4343

app/composables/useStructuredFilters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export function useStructuredFilters(options: UseStructuredFiltersOptions) {
327327
diff = (a.downloads?.weekly ?? 0) - (b.downloads?.weekly ?? 0)
328328
break
329329
case 'updated':
330-
diff = new Date(a.package.date).getTime() - new Date(b.package.date).getTime()
330+
diff = Date.parse(a.package.date) - Date.parse(b.package.date)
331331
break
332332
case 'name':
333333
diff = a.package.name.localeCompare(b.package.name)

app/pages/search.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const displayResults = computed(() => {
234234
diff = (a.downloads?.weekly ?? 0) - (b.downloads?.weekly ?? 0)
235235
break
236236
case 'updated':
237-
diff = new Date(a.package.date).getTime() - new Date(b.package.date).getTime()
237+
diff = Date.parse(a.package.date) - Date.parse(b.package.date)
238238
break
239239
case 'name':
240240
diff = a.package.name.localeCompare(b.package.name)

modules/blog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async function loadBlogPosts(blogDir: string, imagesDir: string): Promise<BlogPo
138138
}
139139

140140
// Sort newest first
141-
posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())
141+
posts.sort((a, b) => Date.parse(b.date) - Date.parse(a.date))
142142
return posts
143143
}
144144

scripts/generate-fixtures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function slimPackument(pkg: Record<string, unknown>): Record<string, unknown> {
183183
const timeA = time[a]
184184
const timeB = time[b]
185185
if (!timeA || !timeB) return 0
186-
return new Date(timeB).getTime() - new Date(timeA).getTime()
186+
return Date.parse(timeB) - Date.parse(timeA)
187187
})
188188
.slice(0, RECENT_VERSIONS_COUNT)
189189

server/api/atproto/bluesky-comments.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function parseThread(
132132
if (parsed) replies.push(parsed)
133133
}
134134
}
135-
replies.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
135+
replies.sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt))
136136
}
137137

138138
return {

shared/utils/atproto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const simpleHash = (str: string): number => {
1515

1616
// Parse date from frontmatter, add slug-path entropy for same-date collision resolution
1717
export const generateBlogTID = (dateString: string, slug: string): string => {
18-
let timestamp = new Date(dateString).getTime()
18+
let timestamp = Date.parse(dateString)
1919

2020
if (timestamp % ONE_DAY_MILLISECONDS === 0) {
2121
const offset = simpleHash(slug) % 1000000
@@ -28,4 +28,4 @@ export const generateBlogTID = (dateString: string, slug: string): string => {
2828

2929
// Using our release date as the tid for the publication
3030
export const npmxPublicationRkey = () =>
31-
TID.create(new Date('2026-03-03').getTime() * MS_TO_MICROSECONDS, TID_CLOCK_ID)
31+
TID.create(Date.parse('2026-03-03') * MS_TO_MICROSECONDS, TID_CLOCK_ID)

test/nuxt/composables/use-package-comparison.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('usePackageComparison', () => {
8989

9090
// Should use version-specific timestamp, NOT time.modified
9191
expect(values[0]!.display).toBe('2024-06-15T00:00:00.000Z')
92-
expect(values[0]!.raw).toBe(new Date('2024-06-15T00:00:00.000Z').getTime())
92+
expect(values[0]!.raw).toBe(Date.parse('2024-06-15T00:00:00.000Z'))
9393
})
9494

9595
it('stores version-specific time in metadata', async () => {

test/unit/app/utils/download-anomalies.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function month(monthStr: string, value: number): MonthlyDataPoint {
2222
return {
2323
value,
2424
month: monthStr,
25-
timestamp: new Date(`${monthStr}-01T00:00:00Z`).getTime(),
25+
timestamp: Date.parse(`${monthStr}-01T00:00:00Z`),
2626
}
2727
}
2828

2929
function year(yearStr: string, value: number): YearlyDataPoint {
3030
return {
3131
value,
3232
year: yearStr,
33-
timestamp: new Date(`${yearStr}-01-01T00:00:00Z`).getTime(),
33+
timestamp: Date.parse(`${yearStr}-01-01T00:00:00Z`),
3434
}
3535
}
3636

0 commit comments

Comments
 (0)