---
title: "How would you limit memory use when paginating a large object store result set?"  
description: "How would you limit memory use when paginating a large object store result set?"  
author: "ICSM Computer"  
published: 2025-07-08  
updated: 2025-07-08  
canonical: https://www.mindstick.com/interview/34328/how-would-you-limit-memory-use-when-paginating-a-large-object-store-result-set  
category: "IndexedDB"  
tags: ["database", "indexeddb"]  
reading_time: 5 minutes  

---

# How would you limit memory use when paginating a large object store result set?

To [**limit memory**](https://www.mindstick.com/interview/34318/what-are-some-best-practices-for-working-with-indexeddb-in-production) **use when paginating a large result set** in IndexedDB (especially with 10,000+ records), avoid loading all data into memory (`.toArray()`) and instead use **streamed access** via **cursors** or **Dexie.js pagination**.

Here’s how you do it efficiently:

## Strategy: Page data without loading everything

### Avoid This:

```javascript
// Bad: loads everything into memory
const all = await db.users.toArray();
const page = all.slice(offset, offset + limit);
```

## Solution 1: Pagination with Dexie.js (Efficient)

If you're using **Dexie.js**, use `offset().limit()`:

```javascript
const pageSize = 100;
const currentPage = 2; // (starts from 0)

const pageData = await db.users
  .orderBy('id') // must be indexed
  .offset(currentPage * pageSize)
  .limit(pageSize)
  .toArray();
```

- Only loads **1 page worth** of records into memory
- Uses **indexed cursor** internally
- Very fast and memory-safe

## Solution 2: Using a Cursor in Vanilla IndexedDB

Cursors allow you to stream records without full memory load:

```javascript
function paginateWithCursor(db, storeName, page, pageSize) {
  return new Promise((resolve, reject) => {
    const tx = db.transaction([storeName], "readonly");
    const store = tx.objectStore(storeName);
    const request = store.openCursor();

    const start = page * pageSize;
    const end = start + pageSize;

    let result = [];
    let count = 0;

    request.onsuccess = function (event) {
      const cursor = event.target.result;
      if (!cursor) return resolve(result);

      if (count >= start && count < end) {
        result.push(cursor.value);
      }

      count++;
      if (count < end) {
        cursor.continue();
      } else {
        resolve(result); // stop early
      }
    };

    request.onerror = () => reject(request.error);
  });
}
```

- Loads only a page-sized portion
- Prevents high memory use
- No need for `.toArray()`

## Tips for Optimal Pagination

| Tip | Why |
| --- | --- |
| Use `.offset().limit()` on indexed field | Indexed traversal avoids full scan |
| Use `orderBy()` only on indexed columns | Otherwise, Dexie can’t paginate efficiently |
| Use `.count()` to get total items without loading them | For pagination UI |
| Avoid `.filter()` or `.map()` after `.toArray()` | Triggers full memory load |

## Optional: Count Total Items

For pagination UI:

```javascript
const totalItems = await db.users.count();
const totalPages = Math.ceil(totalItems / pageSize);
```

## Summary

> To **limit memory use** while paginating in IndexedDB:
>
> - Use `offset().limit()` with indexed `orderBy()` (Dexie)
> - Use a **cursor** to stream only the needed page (Vanilla)
> - **Avoid** `.toArray()` on large datasets

Would you like this turned into a reusable Dexie pagination function with total count?

## Answers

### Answer by ICSM Computer

To [**limit memory**](https://www.mindstick.com/interview/34318/what-are-some-best-practices-for-working-with-indexeddb-in-production) **use when paginating a large result set** in IndexedDB (especially with 10,000+ records), avoid loading all data into memory (`.toArray()`) and instead use **streamed access** via **cursors** or **Dexie.js pagination**.

Here’s how you do it efficiently:

## Strategy: Page data without loading everything

### Avoid This:

```javascript
// Bad: loads everything into memory
const all = await db.users.toArray();
const page = all.slice(offset, offset + limit);
```

## Solution 1: Pagination with Dexie.js (Efficient)

If you're using **Dexie.js**, use `offset().limit()`:

```javascript
const pageSize = 100;
const currentPage = 2; // (starts from 0)

const pageData = await db.users
  .orderBy('id') // must be indexed
  .offset(currentPage * pageSize)
  .limit(pageSize)
  .toArray();
```

- Only loads **1 page worth** of records into memory
- Uses **indexed cursor** internally
- Very fast and memory-safe

## Solution 2: Using a Cursor in Vanilla IndexedDB

Cursors allow you to stream records without full memory load:

```javascript
function paginateWithCursor(db, storeName, page, pageSize) {
  return new Promise((resolve, reject) => {
    const tx = db.transaction([storeName], "readonly");
    const store = tx.objectStore(storeName);
    const request = store.openCursor();

    const start = page * pageSize;
    const end = start + pageSize;

    let result = [];
    let count = 0;

    request.onsuccess = function (event) {
      const cursor = event.target.result;
      if (!cursor) return resolve(result);

      if (count >= start && count < end) {
        result.push(cursor.value);
      }

      count++;
      if (count < end) {
        cursor.continue();
      } else {
        resolve(result); // stop early
      }
    };

    request.onerror = () => reject(request.error);
  });
}
```

- Loads only a page-sized portion
- Prevents high memory use
- No need for `.toArray()`

## Tips for Optimal Pagination

| Tip | Why |
| --- | --- |
| Use `.offset().limit()` on indexed field | Indexed traversal avoids full scan |
| Use `orderBy()` only on indexed columns | Otherwise, Dexie can’t paginate efficiently |
| Use `.count()` to get total items without loading them | For pagination UI |
| Avoid `.filter()` or `.map()` after `.toArray()` | Triggers full memory load |

## Optional: Count Total Items

For pagination UI:

```javascript
const totalItems = await db.users.count();
const totalPages = Math.ceil(totalItems / pageSize);
```

## Summary

> To **limit memory use** while paginating in IndexedDB:
>
> - Use `offset().limit()` with indexed `orderBy()` (Dexie)
> - Use a **cursor** to stream only the needed page (Vanilla)
> - **Avoid** `.toArray()` on large datasets

Would you like this turned into a reusable Dexie pagination function with total count?


---

Original Source: https://www.mindstick.com/interview/34328/how-would-you-limit-memory-use-when-paginating-a-large-object-store-result-set

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
