I am faced with the challenge of replicating a segment of a server-side database for processing within a typescript web application. My goal is to access specific records by their integer ID in typescript. The issue at hand is that the indices may not be sequential, and they might not even start at 0 (they could potentially begin with a much higher value if certain parts of the database have been removed).
Consider the code snippet below:
let a: Array<number> = new Array<number>();
a[10] = 1;
a[11] = 2;
Upon inspecting variable 'a' in Chrome, I notice the following output:
(12) [empty × 10, 1, 2]
This observation suggests that memory has been reserved for 12 values, even though only the last two are being utilized. While this may not pose a significant concern for small starting indices, it can lead to substantial memory waste when dealing with millions as the initial index value and larger objects within the array.
Is my assumption that memory is allocated for unused indices correct? If so, what alternative container would be more suitable for handling non-contiguous indices?