I have an array of random data containing pairs of numbers.
[{width: 123.89000000, length: 4.50},{width: 23.45360, length: 7.20}, ...]
I want to look up the width to get the length in O(1) time and I want the array to be sorted by width.
What is the best way data structure to store this in Javascript / NodeJS? I tested
var hashtable = {};
hashtable[17400.23400000] = { width: 17400.23400000, length: 4.5 };
console.log(hashtable[17400.234]); // check that the key is treated like a real number
It seems to work, but does it?
EDIT:
To clarify the requirement, let me explain the situation:
I am getting the width and length data from a websocket in real-time. The width is discrete up to 4 decimal places, and I need to update the corresponding length as the data streams in, sometimes adding a new width, and sometimes removing an existing width. So for this I need a fast solution for lookup.
Then on every indeterminate x number of milliseconds, I need to return a snapshot of this array of width and length as a sorted array. The number of pairs in this array is maybe around 260,000. Ideally, x should be as small as possible.
Currently I am using a hashtable and using lodash to sort upon request, but I am wondering if there is a faster data structure that is suitable.