Skip to content

Commit

Permalink
perf: optimize fromString()
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Aug 4, 2023
1 parent e9e1aed commit ea0b51c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
7 changes: 2 additions & 5 deletions CalendarDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,16 @@ class CalendarDate {
public static fromString(str: string): CalendarDate {
/** Helper to get an int from a substring of a string, defaulting to two digits long. */
const extractInt = (someString: string, start: number, end?: number) =>
parseInt(someString.substring(start, end ?? start + 2), 10);
Number(someString.substring(start, end ?? start + 2));
const year = extractInt(str, 0, 4);
let month = NaN;
let day = NaN;
if (str.length === 10 && str.charAt(4) === "-" && str.charAt(7) === "-") {
// YYYY-MM-DD format, presumably:
month = extractInt(str, 5);
day = extractInt(str, 8);
} else if (
str.length === 8 && String(parseInt(str, 10)).padStart(8, "0") === str
) {
} else if (str.length === 8) {
// YYYYMMDD format, presumably.
// (Note we check 'String(parseInt(str, 10)).padStart(8, "0") === str' to avoid matching things like '05/05/05')
month = extractInt(str, 4);
day = extractInt(str, 6);
}
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ The benefits of using `CalendarDate` are:
about whether you're using a calendar date or a timestamp.
- It lets you avoid all kinds of subtle bugs related to timezones and daylight
savings time (this is the voice of experience talking!).
- This implementation is very optimized and is typically 3-10x faster than using
the native `Date` class. It represents all dates as a single `Number` so it is
- This implementation is very optimized and is typically 6-9x faster than using
the native `Date` class. It represents each date as a single `Number` so it is
also very memory efficient.
- Unlike `Date`, it is immutable, which makes your code more predictable.

Expand Down Expand Up @@ -106,10 +106,10 @@ format.

On this test, `CalendarDate` is:

- **5x faster** than the native `Date` object
- **7x faster** than
- **6x faster** than the native `Date` object
- **8x faster** than
[`calendar-date` on NPM](https://www.npmjs.com/package/calendar-date)
- **17x faster** than [Day.js](https://day.js.org/)
- **18x faster** than [Day.js](https://day.js.org/)

### Parsing

Expand All @@ -118,10 +118,10 @@ API response.

On this test, `CalendarDate` is:

- Slightly (1.13x) slower than the native `Date` object
- **4x faster** than
- **1.25x faster** than the native `Date` object
- **6x faster** than
[`calendar-date` on NPM](https://www.npmjs.com/package/calendar-date)
- **4x faster** than [Day.js](https://day.js.org/)
- **6x faster** than [Day.js](https://day.js.org/)

### Iterate throught a year

Expand All @@ -130,10 +130,10 @@ year, converting each date to an ISO 8601 string.

On this test, `CalendarDate` is:

- **8x faster** than the native `Date` object
- **18x faster** than
- **9x faster** than the native `Date` object
- **20x faster** than
[`calendar-date` on NPM](https://www.npmjs.com/package/calendar-date)
- **32x faster** than [Day.js](https://day.js.org/)
- **35x faster** than [Day.js](https://day.js.org/)

## FAQ

Expand Down

0 comments on commit ea0b51c

Please sign in to comment.