<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys" -->

---
title: Building an Offline-First Spaced Repetition Engine with...
description: A hands-on tutorial walks through building the foundation of a spaced-repetition flashcard engine in vanilla JavaScript, using IndexedDB for structured local...
canonical: https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB | daily.dev
og:description: A hands-on tutorial walks through building the foundation of a spaced-repetition flashcard engine in vanilla JavaScript, using IndexedDB for structured local...
og:url: https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys
og:image: https://api.daily.dev/og/posts/S5ZDPbzys.png
og:image:alt: Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB

**[SitePoint](https://daily.dev/sources/sitepoint)** · 19 min read · 3 upvotes · 0 comments

## Summary

A hands-on tutorial walks through building the foundation of a spaced-repetition flashcard engine in vanilla JavaScript, using IndexedDB for structured local storage. It covers designing a card data model, writing a deterministic scheduling function (testable via injected timestamps), building an IndexedDB repository with a dueAt index for efficient queries, wrapping transactions for atomic card+review writes, and separating concerns across a review session, UI layer, service worker for offline shell caching, and a future sync layer using event-sourced review history for conflict resolution. It also touches on daily workload caps, Fisher–Yates shuffling, XSS-safe rendering, and avoiding metric-driven UX manipulation.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb>

## Questions this post answers

### How do I query only the IndexedDB records that are due right now without loading the whole store into memory?

Create an index on the due-date field of the object store, then use IDBKeyRange.upperBound(now) with index.getAll() to retrieve only records whose due date has passed. This avoids fetching every record and filtering in JavaScript, letting the database perform the query directly, which scales far better for large card decks.

_Developers optimizing IndexedDB-backed apps can find implementation patterns like this shared on daily.dev._

### Why should I pass the current time as a parameter into a scheduling function instead of calling Date.now() inside it?

Passing the current time as an explicit argument makes the function pure and deterministic, so unit tests can control time instead of depending on the real clock. Calling Date.now() directly inside business logic like a spaced-repetition scheduler hides a dependency on wall-clock time and makes reliable, repeatable test assertions on due dates and intervals much harder to write.

_Testing-focused developers often track patterns for writing deterministic, time-dependent unit tests via daily.dev._

### Why is synchronizing individual review events better than syncing only the final card state when a flashcard is studied on two devices?

Syncing only the final card state, like an interval value of 5 on one device and 8 on another, gives the server no way to reconcile which is correct. Instead, synchronizing individual review events (each with a cardId, rating, and timestamp) preserves the full history, similar to a small event-sourcing pattern, so the server can reconstruct and resolve conflicting states across devices.

_Developers designing multi-device sync and conflict resolution often research event-sourcing patterns through daily.dev._

## Similar posts on daily.dev

- [Synchronize MySQL and IndexedDB in an Offline-First App](https://daily.dev/posts/synchronize-mysql-and-indexeddb-in-an-offline-first-app-t7qnof3ch) · Planet MySQL · 0 upvotes · 0 comments
- [Offline-first frontend apps in 2025: IndexedDB and SQLite in the browser and beyond](https://daily.dev/posts/offline-first-frontend-apps-in-2025-indexeddb-and-sqlite-in-the-browser-and-beyond-djyieqljy) · LogRocket · 1 upvotes · 0 comments
- [Building a browser API in one shot](https://daily.dev/posts/building-a-browser-api-in-one-shot-ulgi5fqya) · Read the Tea Leaves · 43 upvotes · 0 comments
- [Best Ways Developers Study New Frameworks Using Flashcards](https://daily.dev/posts/best-ways-developers-study-new-frameworks-using-flashcards-f7153nrhf) · SitePoint · 6 upvotes · 0 comments
- [Storing Requests In Indexeddb](https://daily.dev/posts/storing-requests-in-indexeddb-07wzam6nk) · Larablog · 0 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript), [#architecture](https://daily.dev/tags/architecture), [#offline-first](https://daily.dev/tags/offline-first)

[View this post on daily.dev](https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB","url":"https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys"},"datePublished":"2026-08-27T15:10:28.060Z","dateModified":"2026-09-14T06:08:10.242Z","description":"A hands-on tutorial walks through building the foundation of a spaced-repetition flashcard engine in vanilla JavaScript, using IndexedDB for structured local...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/81ccc7490476ab1f46b7e53d4ca0ceae?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/81ccc7490476ab1f46b7e53d4ca0ceae?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,javascript,architecture,offline-first","timeRequired":"PT19M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Building an Offline-First Spaced Repetition Engine with JavaScript and IndexedDB"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/building-an-offline-first-spaced-repetition-engine-with-javascript-and-indexeddb-s5zdpbzys#faq","mainEntity":[{"@type":"Question","name":"How do I query only the IndexedDB records that are due right now without loading the whole store into memory?","acceptedAnswer":{"@type":"Answer","text":"Create an index on the due-date field of the object store, then use IDBKeyRange.upperBound(now) with index.getAll() to retrieve only records whose due date has passed. This avoids fetching every record and filtering in JavaScript, letting the database perform the query directly, which scales far better for large card decks. Developers optimizing IndexedDB-backed apps can find implementation patterns like this shared on daily.dev."}},{"@type":"Question","name":"Why should I pass the current time as a parameter into a scheduling function instead of calling Date.now() inside it?","acceptedAnswer":{"@type":"Answer","text":"Passing the current time as an explicit argument makes the function pure and deterministic, so unit tests can control time instead of depending on the real clock. Calling Date.now() directly inside business logic like a spaced-repetition scheduler hides a dependency on wall-clock time and makes reliable, repeatable test assertions on due dates and intervals much harder to write. Testing-focused developers often track patterns for writing deterministic, time-dependent unit tests via daily.dev."}},{"@type":"Question","name":"Why is synchronizing individual review events better than syncing only the final card state when a flashcard is studied on two devices?","acceptedAnswer":{"@type":"Answer","text":"Syncing only the final card state, like an interval value of 5 on one device and 8 on another, gives the server no way to reconcile which is correct. Instead, synchronizing individual review events (each with a cardId, rating, and timestamp) preserves the full history, similar to a small event-sourcing pattern, so the server can reconstruct and resolve conflicting states across devices. Developers designing multi-device sync and conflict resolution often research event-sourcing patterns through daily.dev."}}]}
```

