Skip to content

This application represents a demo app to demonstrate how different pagination work, and how indexing, caching, and other strategies helps in imporving performance

Notifications You must be signed in to change notification settings

Prasundas99/Pagination-MERN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

Pagination Demo application

This application represents a demo app to demonstrate how different pagination work, and how indexing, caching, and other strategies helps in imporving performance

In the world of pagination strategies, two key methods emerge with distinct advantages and trade-offs: Offset-based pagination, a classic and straightforward approach, and Cursor-based pagination, an optimized method for handling large, frequently updated datasets.

Offset-Based Pagination

Offset-based pagination functions similarly to traditional page numbers in a book, where each page represents a specific position in a dataset. This approach is typically implemented by specifying an offset to skip a defined number of records and then retrieving a fixed number of items from that point.

Example Implementation:

const posts = await Post.find()
  .sort({ createdAt: -1 })
  .skip((page - 1) * limit)
  .limit(limit)
  .lean();

This method is intuitive and ideal for smaller datasets, as it allows users to jump to specific "pages" of data quickly. However, as the dataset grows, the time required to retrieve specific pages increases, potentially degrading performance. For instance, skipping through large amounts of data becomes increasingly inefficient, as the system must count through all prior records to reach the desired offset.

Cursor-Based Pagination

Cursor-based pagination, on the other hand, uses a unique identifier (often the last item’s ID from the previous page) to indicate the start point for retrieving subsequent records. Instead of counting records, this approach uses a marker to "bookmark" where the last retrieval left off, resulting in faster, more consistent performance with large or dynamically changing datasets.

Example Implementation:

let query = {};
if (lastItemId) {
  query = { _id: { $gt: lastItemId } };
}
const posts = await Post.find(query)
  .sort({ createdAt: -1 })
  .limit(limit)
  .lean();

Cursor-based pagination minimizes the need for costly record counts and is less affected by dataset size. It is especially effective when handling real-time data that is subject to frequent updates, as it reduces the likelihood of data inconsistency.

Choosing Between Offset and Cursor Pagination

Selecting the appropriate pagination method depends on the specific requirements of the application:

Choose Offset-Based Pagination if:

  • The dataset is relatively small.
  • There’s a need for easy implementation, particularly in prototypes.
  • Direct navigation to specific pages is required.
  • Data is relatively static, with infrequent updates.

Choose Cursor-Based Pagination if:

  • The dataset is large and grows over time.
  • Real-time data updates are frequent.
  • Performance and consistency in dynamic environments are critical

About

This application represents a demo app to demonstrate how different pagination work, and how indexing, caching, and other strategies helps in imporving performance

Topics

Resources

Stars

Watchers

Forks