diff --git a/examples/optimize.js b/examples/optimize.js index 01af63c..b36a7e9 100644 --- a/examples/optimize.js +++ b/examples/optimize.js @@ -38,30 +38,124 @@ function createReviewHistoriesForCards() { const reviewHistories = [ [ - [new Date('2023-01-01'), 3], - [new Date('2023-01-02'), 4], - [new Date('2023-01-05'), 3], - [new Date('2023-01-15'), 4], - [new Date('2023-02-01'), 3], - [new Date('2023-02-20'), 4], + [new Date("2023-01-01"), 3], + [new Date("2023-01-02"), 4], + [new Date("2023-01-05"), 3], + [new Date("2023-01-15"), 4], + [new Date("2023-02-01"), 3], + [new Date("2023-02-20"), 4], ], [ - [new Date('2023-01-01'), 2], - [new Date('2023-01-02'), 3], - [new Date('2023-01-04'), 4], - [new Date('2023-01-12'), 3], - [new Date('2023-01-28'), 4], - [new Date('2023-02-15'), 3], - [new Date('2023-03-05'), 4], + [new Date("2023-01-01"), 2], + [new Date("2023-01-02"), 3], + [new Date("2023-01-04"), 4], + [new Date("2023-01-12"), 3], + [new Date("2023-01-28"), 4], + [new Date("2023-02-15"), 3], + [new Date("2023-03-05"), 4], ], [ - [new Date('2023-01-01'), 4], - [new Date('2023-01-08'), 4], - [new Date('2023-01-24'), 3], - [new Date('2023-02-10'), 4], - [new Date('2023-03-01'), 3], + [new Date("2023-01-01"), 4], + [new Date("2023-01-08"), 4], + [new Date("2023-01-24"), 3], + [new Date("2023-02-10"), 4], + [new Date("2023-03-01"), 3], + ], + [ + [new Date("2023-01-01"), 1], + [new Date("2023-01-02"), 1], + [new Date("2023-01-03"), 3], + [new Date("2023-01-06"), 4], + [new Date("2023-01-16"), 4], + [new Date("2023-02-01"), 3], + [new Date("2023-02-20"), 4], + ], + [ + [new Date("2023-01-01"), 3], + [new Date("2023-01-03"), 3], + [new Date("2023-01-08"), 2], + [new Date("2023-01-10"), 4], + [new Date("2023-01-22"), 3], + [new Date("2023-02-05"), 4], + [new Date("2023-02-25"), 3], + ], + [ + [new Date("2023-01-01"), 4], + [new Date("2023-01-09"), 3], + [new Date("2023-01-19"), 4], + [new Date("2023-02-05"), 3], + [new Date("2023-02-25"), 4], + ], + [ + [new Date("2023-01-01"), 2], + [new Date("2023-01-02"), 3], + [new Date("2023-01-05"), 4], + [new Date("2023-01-15"), 3], + [new Date("2023-01-30"), 4], + [new Date("2023-02-15"), 3], + [new Date("2023-03-05"), 4], + ], + [ + [new Date("2023-01-01"), 3], + [new Date("2023-01-04"), 4], + [new Date("2023-01-14"), 4], + [new Date("2023-02-01"), 3], + [new Date("2023-02-20"), 4], + ], + [ + [new Date("2023-01-01"), 1], + [new Date("2023-01-01"), 3], + [new Date("2023-01-02"), 1], + [new Date("2023-01-02"), 3], + [new Date("2023-01-03"), 3], + [new Date("2023-01-07"), 3], + [new Date("2023-01-15"), 4], + [new Date("2023-01-31"), 3], + [new Date("2023-02-15"), 4], + [new Date("2023-03-05"), 3], + ], + [ + [new Date("2023-01-01"), 4], + [new Date("2023-01-10"), 3], + [new Date("2023-01-20"), 4], + [new Date("2023-02-05"), 4], + [new Date("2023-02-25"), 3], + [new Date("2023-03-15"), 4], + ], + [ + [new Date("2023-01-01"), 1], + [new Date("2023-01-02"), 2], + [new Date("2023-01-03"), 3], + [new Date("2023-01-04"), 4], + [new Date("2023-01-10"), 3], + [new Date("2023-01-20"), 4], + [new Date("2023-02-05"), 3], + [new Date("2023-02-25"), 4], + ], + [ + [new Date("2023-01-01"), 3], + [new Date("2023-01-05"), 4], + [new Date("2023-01-15"), 3], + [new Date("2023-01-30"), 4], + [new Date("2023-02-15"), 3], + [new Date("2023-03-05"), 4], + ], + [ + [new Date("2023-01-01"), 2], + [new Date("2023-01-03"), 3], + [new Date("2023-01-07"), 4], + [new Date("2023-01-17"), 3], + [new Date("2023-02-01"), 4], + [new Date("2023-02-20"), 3], + [new Date("2023-03-10"), 4], + ], + [ + [new Date("2023-01-01"), 4], + [new Date("2023-01-12"), 3], + [new Date("2023-01-25"), 4], + [new Date("2023-02-10"), 3], + [new Date("2023-03-01"), 4], ], - // ... add more review history arrays ] // Cycle and repeat the array to create a larger dataset of 100 cards const res = [] @@ -85,7 +179,7 @@ function convertToFSRSItem(history) { for (const [date, rating] of history) { - const deltaT = (date.getTime() - lastDate.getTime()) / (1000 * 60 * 60 * 24) // Calculate difference in days + const deltaT = dateDiffInDays(lastDate, date) reviews.push(new FSRSReview(rating, deltaT)) items.push(new FSRSItem([...reviews])) lastDate = date @@ -94,4 +188,13 @@ function convertToFSRSItem(history) { return items.filter((item) => item.longTermReviewCnt() > 0) } +function dateDiffInDays(a, b) { + const _MS_PER_DAY = 1000 * 60 * 60 * 24; + // Discard the time and time-zone information. + const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate()); + const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate()); + + return Math.floor((utc2 - utc1) / _MS_PER_DAY) +} + main()