There has been a lot of confusion so I have rewritten the question to be more clear. I will describe the entire process of how the requests are dealt with, however this question is only related to the first part of the process.
I have an object called requests that is initialised empty. Other modules within my node.js server add request objects that are in the format as below:
requests["name of request"] = {
requestType: "type of request",
parameters: {
"request parameters here"
},
callback: function(data){//Do stuff with the data}
}
Here is how requests like this are processed.
All requests MUST make a call to an external JSON API that is rate limited. I can group individual requests up into groups of size 1-40, regardless of the size of the group it will only count as 1 request against my rate limit. Therefore to maximise my use of the rate limit I should try to make groups that are as large as possible while not delaying the processing of individual requests.
Since the maximum group size is 40 I need to get 40 requests from the requests object and copy then into a second object called inProgressRequests leaving any remaining objects untouched. I then want to delete them to prevent the requests being processed more than once.
Once this has been done I can make the call to the external API, processing all 40 requests at once. I receive the data in bulk and then I must split the data into 40 sections. I then need to return the right data section to the right request's callback function as the data parameter.
If I were to use an array then for each chunk of data (of which there are 40) I would have to loop through the array to find the correct request and then return its chunk of data to it. This would require me to loop through the array a total of 40 times, which is inefficient. Therefore I have decided to use an object, as then I can access each request more efficiently like so: requests[requestName]
Therefore using arrays is not an option unless there is a more efficient way to search for request objects in an unordered array that I have not though of.
My question is what is the most time and resource efficient way for me to perform the first part of processing the request? That is to say what is the most effective way for me to get the first 40 individual request objects from the requests object and then delete them from the requests object?
requestNamematches. If there are 40 requests at a time like in my example that would require me to loop through the array 40 times and that would be very time consuming