SuperMemo

repository·master·Indexed 43 days ago

https://github.com/viendinhcom/supermemo

A JavaScript/TypeScript implementation of the SM2 (SuperMemo 2) algorithm for spaced repetition flashcard systems. It provides the supermemo function to calculate updated intervals, repetitions, and easiness factors based on a user's recall grade (0-5).

Tokens
1.6K
Snippets
7
Records
7
Agent score
6%

What's inside SuperMemo

  1. Install SuperMemo

    master

    You can install SuperMemo in various environments:

    Node.js

    Using npm:

    npm install --save supermemo

    CommonJS:

    const { supermemo } = require('supermemo');

    ES Modules:

    import { supermemo, SuperMemoItem, SuperMemoGrade } from 'supermemo';

    Deno

    Using JSR:

    deno add jsr:@vien/supermemo

    Importing via JSR:

    import { supermemo, SuperMemoItem, SuperMemoGrade } from '@vien/supermemo';
    // OR
    import { supermemo, SuperMemoItem, SuperMemoGrade } from 'jsr:@vien/supermemo';

    Browser

    Using esm.sh:

    import { supermemo } from 'https://esm.sh/supermemo@2';
    npm install --save supermemo
  2. Implement SuperMemo with DayJS for scheduling

    master

    To use SuperMemo for actual scheduling, combine the supermemo output with a date library like dayjs to calculate the next dueDate based on the returned interval.

    import dayjs from 'dayjs';
    import { supermemo, SuperMemoItem, SuperMemoGrade } from 'supermemo';
    
    interface Flashcard extends SuperMemoItem {
      front: string;
      back: string;
      dueDate: string;
    }
    
    function practice(flashcard: Flashcard, grade: SuperMemoGrade): Flashcard {
      const { interval, repetition, efactor } = supermemo(flashcard, grade);
    
      // Calculate next due date by adding the interval (in days) to the current time
      const dueDate = dayjs(Date.now()).add(interval, 'day').toISOString();
    
      return { ...flashcard, interval, repetition, efactor, dueDate };
    }
    
    let flashcard: Flashcard = {
      front: 'programer',
      back: 'an organism that turns caffeine in software',
      interval: 0,
      repetition: 0,
      efactor: 2.5,
      dueDate: dayjs(Date.now()).toISOString(),
    };
    
    // Update flashcard based on a grade of 5
    flashcard = practice(flashcard, 5);
  3. Use the supermemo function

    master

    The supermemo function implements the SM2 algorithm to calculate the next state of a flashcard item based on a user's grade.

    Signature: supermemo(item: SuperMemoItem, grade: SuperMemoGrade): SuperMemoItem

    Parameters:

    • item: A SuperMemoItem object containing the current state.
    • grade: A SuperMemoGrade (0-5) representing the user's response quality.

    Returns: A new SuperMemoItem with updated interval, repetition, and efactor values.

    let item: SuperMemoItem = {
      interval: 0,
      repetition: 0,
      efactor: 2.5,
    };
    
    item = supermemo(item, 5);
  4. Understand SuperMemoItem and SuperMemoGrade

    master

    SuperMemoItem

    An object representing the state of a flashcard in the spaced repetition cycle.

    • repetition (number): The number of continuous correct responses. Initial value should be 0.
    • interval (number): The inter-repetition interval in days. Initial value should be 0.
    • efactor (number): The easiness factor reflecting how easy the item is to retain. Initial value should be 2.5.

    SuperMemoGrade

    A numeric value from 0 to 5 representing the quality of the response:

    • 5: perfect response.
    • 4: correct response after a hesitation.
    • 3: correct response recalled with serious difficulty.
    • 2: incorrect response; where the correct one seemed easy to recall.
    • 1: incorrect response; the correct one remembered.
    • 0: complete blackout.
    type SuperMemoItem = {
      interval: number;
      repetition: number;
      efactor: number;
    };
    
    type SuperMemoGrade = 0 | 1 | 2 | 3 | 4 | 5;
  5. Use the supermemo() function to update learning progress

    master

    The supermemo() function implements the SM-2 algorithm to calculate the next review interval, repetition count, and ease factor for a learning item based on a user's recall grade.

    It takes a SuperMemoItem and a SuperMemoGrade and returns a new SuperMemoItem with updated values. This is the primary entry point for managing spaced repetition schedules.

    import supermemo, { SuperMemoItem } from 'supermemo';
    
    const currentItem: SuperMemoItem = {
      interval: 1,
      repetition: 1,
      efactor: 2.5
    };
    
    // Grade 5 represents a perfect recall
    const updatedItem = supermemo(currentItem, 5);
  6. Use SuperMemoGrade for recall scoring

    master

    The SuperMemoGrade type defines the possible scores a user can give during a review session. The algorithm interprets these scores as follows:

    • 0 to 2: Failure to recall. This resets the repetition count to 0 and sets the interval to 1 day.
    • 3 to 5: Successful recall. This progresses the repetition count and increases the interval based on the efactor.
    export type SuperMemoGrade = 0 | 1 | 2 | 3 | 4 | 5;
  7. Define SuperMemoItem parameters

    master

    A SuperMemoItem object tracks the state of a single piece of information within the algorithm. It requires three numeric properties:

    • interval: The number of days until the next review.
    • repetition: The number of times the item has been successfully recalled in a row.
    • efactor: The ease factor, which determines how quickly the interval increases (lower values mean more frequent reviews).
    export type SuperMemoItem = {
      interval: number;
      repetition: number;
      efactor: number;
    };