Skip to content

[JITERA] Optimize getBookById Function in bookService.js

chi le requested to merge feat/optimize-getBookById-1749524732 into main

Created by: chi-jitera

Overview

This pull request introduces optimizations to the getBookById function in the bookService.js file to enhance the performance of book retrieval by ID. The changes focus on utilizing Mongoose's .lean() method to return plain JavaScript objects, which reduces overhead and improves response times.

Changes

  1. Use of .lean():
    • The getBookById function has been modified to include the .lean() method in the Mongoose query. This change allows the function to return plain JavaScript objects instead of Mongoose documents, which is beneficial when the full Mongoose document functionality is not required.
    • This optimization is particularly useful for read-heavy operations, as it minimizes the memory footprint and speeds up the retrieval process.

Code Changes

exports.getBookById = async (bookId) => {
    const book = await Book.findById(bookId).lean();
    if (!book) throw new Error('Book not found');
    return book;
};

By implementing these changes, we ensure that the retrieval of books by ID is more efficient, leading to better performance in our application.

Merge request reports