如何优化数字范围关联ID的查找及存储机制?
Hey there! Let's tackle this problem of mapping numeric ranges to IDs more efficiently. Storing ranges as object keys definitely gets messy and slow once you have a lot of ranges—looking up a number would mean checking every key one by one, which is O(n) time. Here are some way better approaches:
If your numeric range is limited and not overly large (like 0 to 1000), precomputing an array where the index equals the input number and the value is the corresponding ID is a perfect choice. Lookups here are O(1)—blazingly fast.
- Example implementation:
// Define your range rules first const idLookup = []; // Fill 0-49 with ID 101 for (let i = 0; i < 50; i++) idLookup[i] = 101; // Fill 50-149 with ID 102 for (let i = 50; i < 150; i++) idLookup[i] = 102; // Fill 150-499 with ID 47 for (let i = 150; i < 500; i++) idLookup[i] = 47; // Lookup function function getID(num) { if (num >= 500) return 123; // Handle upper bound range return idLookup[num]; }
- Pros: Near-instant lookups, dead-simple to implement.
- Cons: Wastes memory if your numeric range is extremely large (like 0 to 1,000,000).
Store each range as an object with start, end, and id properties in a sorted array, then use binary search to quickly locate which range the input number falls into. This cuts lookup time to O(log n), which is way more efficient than full iteration.
- Make sure your array is sorted by the
startvalue first (sort once if your ranges are static). - Example implementation:
// Sorted array of range objects const ranges = [ { start: 0, end: 49, id: 101 }, { start: 50, end: 149, id: 102 }, { start: 150, end: 499, id: 47 }, { start: 500, end: Infinity, id: 123 } ]; function getID(num) { let left = 0; let right = ranges.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); const currentRange = ranges[mid]; if (num >= currentRange.start && num <= currentRange.end) { return currentRange.id; } else if (num < currentRange.start) { right = mid - 1; } else { left = mid + 1; } } // Return default or throw error if no range matches return null; }
- Pros: Minimal memory usage, works for any size of range, fast lookups.
- Cons: Requires the array to stay sorted, implementation is slightly more involved than the lookup array.
If you need to frequently add, remove, or modify ranges, an interval tree (a type of balanced binary search tree optimized for range queries) is the way to go. That said, for most frontend or basic backend use cases, the above two methods are sufficient—this is only necessary for complex dynamic range scenarios.
| Approach | Lookup Time Complexity | Space Complexity | Ideal Use Case |
|---|---|---|---|
| Precomputed Array | O(1) | O(max_number) | Small, fixed numeric ranges |
| Sorted Ranges + Binary Search | O(log n) | O(n) | Large, static or infrequently updated ranges |
| Interval Tree | O(log n + k) | O(n) | Dynamic ranges with frequent additions/deletions |
Pick the approach that fits your specific needs—binary search is usually the sweet spot for most scenarios where you don't want to waste memory on a giant lookup array.
内容的提问来源于stack exchange,提问作者Baterka

