最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Optimizing availability check for a booking system - Stack Overflow

programmeradmin3浏览0评论

I'm working on a resource booking system and need advice on optimizing my logic for checking resource availability. The current implementation works but feels inefficient since it performs unnecessary checks.

Here’s the scenario:

  1. I have a list of resources (e.g., devices, rooms, or any bookable item) stored in resourceList.
  2. Each resource can have multiple bookings, stored in a bookingList.
  3. When a booking request comes in, I need to check if the requested dates overlap with any existing bookings for that resource. If the resource is available, I add it to the list of confirmed bookings. Each booking has a start date and end date.

Here’s my current code:

// Sample resource list
const resourceList = [
  { resourceId: "R1", status: "Available" },
  { resourceId: "R2", status: "Booked" },
  { resourceId: "R3", status: "Booked" }
];

// Sample booking list
const bookingList = [
  { resourceId: "R2", startDate: "2025-01-10", endDate: "2025-01-15" },
  { resourceId: "R2", startDate: "2025-01-20", endDate: "2025-01-25" },
  { resourceId: "R3", startDate: "2025-01-12", endDate: "2025-01-18" }
];

// Requested dates
const startDate = new Date("2025-01-15");
const endDate = new Date("2025-01-19");

let availableResources = [];
let newBookings = [];
const requiredResources = 1; // Example: Only need 1 resource

for (let resource of resourceList) {
  if (resource.status === "Booked") {
    const resourceBookings = bookingList.filter(
      booking => booking.resourceId === resource.resourceId
    );

    // Check if the resource is available for the requested dates
    const isAvailable = resourceBookings.every(booking => {
      const existingStart = new Date(booking.startDate);
      const existingEnd = new Date(booking.endDate);

      return endDate < existingStart || startDate > existingEnd;
    });

    // Add resource if available and limit not met
    if (isAvailable && availableResources.length < requiredResources) {
      availableResources.push(resource.resourceId);
      newBookings.push({
        resourceId: resource.resourceId,
        startDate: startDate.toISOString().split("T")[0],
        endDate: endDate.toISOString().split("T")[0]
      });
    }
  }
}

My Concerns:

  • Unnecessary checks: I feel like checking each booking should not be the way and there is a better more efficient way to check only a subset of the booking?
  • Performance issues: As the number of resources and bookings grows, this approach might not scale well.

If you’ve tackled a similar problem or have any ideas, I’d love to hear them!

Thank you in advance for your suggestions.

I'm working on a resource booking system and need advice on optimizing my logic for checking resource availability. The current implementation works but feels inefficient since it performs unnecessary checks.

Here’s the scenario:

  1. I have a list of resources (e.g., devices, rooms, or any bookable item) stored in resourceList.
  2. Each resource can have multiple bookings, stored in a bookingList.
  3. When a booking request comes in, I need to check if the requested dates overlap with any existing bookings for that resource. If the resource is available, I add it to the list of confirmed bookings. Each booking has a start date and end date.

Here’s my current code:

// Sample resource list
const resourceList = [
  { resourceId: "R1", status: "Available" },
  { resourceId: "R2", status: "Booked" },
  { resourceId: "R3", status: "Booked" }
];

// Sample booking list
const bookingList = [
  { resourceId: "R2", startDate: "2025-01-10", endDate: "2025-01-15" },
  { resourceId: "R2", startDate: "2025-01-20", endDate: "2025-01-25" },
  { resourceId: "R3", startDate: "2025-01-12", endDate: "2025-01-18" }
];

// Requested dates
const startDate = new Date("2025-01-15");
const endDate = new Date("2025-01-19");

let availableResources = [];
let newBookings = [];
const requiredResources = 1; // Example: Only need 1 resource

for (let resource of resourceList) {
  if (resource.status === "Booked") {
    const resourceBookings = bookingList.filter(
      booking => booking.resourceId === resource.resourceId
    );

    // Check if the resource is available for the requested dates
    const isAvailable = resourceBookings.every(booking => {
      const existingStart = new Date(booking.startDate);
      const existingEnd = new Date(booking.endDate);

      return endDate < existingStart || startDate > existingEnd;
    });

    // Add resource if available and limit not met
    if (isAvailable && availableResources.length < requiredResources) {
      availableResources.push(resource.resourceId);
      newBookings.push({
        resourceId: resource.resourceId,
        startDate: startDate.toISOString().split("T")[0],
        endDate: endDate.toISOString().split("T")[0]
      });
    }
  }
}

My Concerns:

  • Unnecessary checks: I feel like checking each booking should not be the way and there is a better more efficient way to check only a subset of the booking?
  • Performance issues: As the number of resources and bookings grows, this approach might not scale well.

If you’ve tackled a similar problem or have any ideas, I’d love to hear them!

Thank you in advance for your suggestions.

Share Improve this question edited Jan 19 at 19:27 sara ameen asked Jan 19 at 18:59 sara ameensara ameen 212 bronze badges 2
  • Post code as a minimal reproducible example (hint: click the brackets button <>). Also, can you post an example of what resourceList looks like? – zer00ne Commented Jan 19 at 19:09
  • "I'm working on a resource booking system" - does the system have a database? – Bergi Commented Jan 19 at 21:56
Add a comment  | 

1 Answer 1

Reset to default 0

Regarding the 2 concerns you mention, you are correct on both. If number of held reservations is "fairly small" then checking all of them like that is probably OK. Context is everything.

The key to unlocking better performance at scale is using sorted data. Consider if I gave you a row of 10,000 index cards for the reservations and they were sorted by start date. And I asked you to see if there was a spot available on June 5-7, 2026. Then there are only 2 steps which would be very fast:

  1. Find the latest-ending reservation before June 5
  2. look at the next reservation and ensure it is June 8 or greater

So, you don't mention how/where your data is held. If it is held "in memory" somewhere, it can be held/maintained in sorted order via several data structures. One of the things you'd like to avoid is re-sorting the reservations every time you want to check.

If you can achieve that, then the algorithm to determine availability is:

  1. Use Binary Search to locate the previous-ending reservation
  2. Inspect the next reservation to see if it starts after the request

Binary Search is the most efficient way to locate the correct spot in the sorted data. It's easy to implement, and guaranteed O(log(n)) performance.

发布评论

评论列表(0)

  1. 暂无评论