Skip to content

Commit 10c288b

Browse files
committed
fix: speedup dayOfWeek
1 parent 2829037 commit 10c288b

4 files changed

Lines changed: 2067 additions & 1591 deletions

File tree

std/assembly/date.ts

Lines changed: 5 additions & 4 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,7 +361,8 @@ 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);
364+
const century = floorDiv(year >> 2, 100 >> 2);
365+
year += (year >> 2) + (century >> 2) - century;
365366
month = <i32>load<u8>(tab + month - 1);
366367
return euclidRem(year + month + day, 7);
367368
}

0 commit comments

Comments
 (0)