Skip to content

Commit 7bf8c48

Browse files
committed
fix: speedup dayOfWeek
1 parent 2829037 commit 7bf8c48

4 files changed

Lines changed: 2080 additions & 1605 deletions

File tree

std/assembly/date.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class Date {
6262
let timeString: string;
6363
dateString = dateTimeString.substring(0, posT);
6464
timeString = dateTimeString.substring(posT + 1);
65-
65+
6666
// might end with an offset ("Z", "+05:30", "-08:00", etc.)
6767
for (let i = timeString.length - 1; i >= 0; i--) {
6868
let c = timeString.charCodeAt(i);
@@ -82,8 +82,8 @@ export class Date {
8282
} else {
8383
let offsetHours = i32.parse(timeString.substring(i + 1));
8484
offsetMs = offsetHours * MILLIS_PER_HOUR;
85-
}
86-
85+
}
86+
8787
if (c == 45) offsetMs = -offsetMs; // negative offset
8888
timeString = timeString.substring(0, i);
8989
break;
@@ -361,9 +361,12 @@ function dayOfWeek(year: i32, month: i32, day: i32): i32 {
361361
const tab = memory.data<u8>([0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]);
362362

363363
year -= i32(month < 3);
364-
year += floorDiv(year, 4) - floorDiv(year, 100) + floorDiv(year, YEARS_PER_EPOCH);
365-
month = <i32>load<u8>(tab + month - 1);
366-
return euclidRem(year + month + day, 7);
364+
365+
const century = floorDiv(year >> 2, 25);
366+
year += (year >> 2) + (century >> 2) - century;
367+
368+
const monthOffset = <i32>load<u8>(tab + month - 1);
369+
return euclidRem(year + monthOffset + day, 7);
367370
}
368371

369372
function stringify(value: i32, padding: i32 = 2): string {

0 commit comments

Comments
 (0)