See edit too
I'm currently working in JavaScript, but I believe any readable pseudocode may be able to help answer my question.
Describing my issue might be a bit of a challenge, so feel free to ask for clarifications. I'll respond promptly and refine my post accordingly.
Basically, I have an array that functions as a queue. Items are added to the end of the array, and when they are processed, they need to be removed. I'm looking for the fastest way to retrieve and process the first item in the array without having to shift all entries each time. I want to iterate on a first-added basis without concerning myself with the index of the array. It's important to note that I am not looping through the array; instead, I have a main loop in my application that checks if the array contains any items on each iteration. If it does, the data is retrieved and removed from the array. Currently, I am using the pop method for this, but I now realize that I need to retrieve the oldest item in the queue first, not the newest.
For further clarification:
In my application, I have blocks of raw data that need to be processed by a function before they can be used by other parts of the application. Each block has a unique ID that is passed to the function for parsing. For optimization purposes, I only parse the blocks of data as they are required.
My current system involves adding the unique ID of a block to be parsed to an array. A continuous loop in my application constantly checks this array for items. If there are items present, it pops the last item from the array and passes the unique ID to the parsing function.
Due to performance reasons, only one block of data can be parsed on each iteration of the loop. The problem arises when multiple blocks of data are already in the queue array, and new items are added before the loop finishes processing the existing IDs in the array. Essentially, new IDs are added to the end of the array before the existing IDs are cleared out by the loop.
Although new data is required infrequently, when it is needed, a large number of IDs are added at once. This behavior is inherent to the application and cannot be changed.
As I mentioned earlier, I currently use the pop method, which means that the most recently added ID is always parsed first. However, I now realize that I would prefer to parse the oldest items in the queue first.
In summary, I'm looking for a way to iterate through an array from oldest to newest without reorganizing the array each time. The index of the array is not significant to me; I simply require a first-added, first-parsed approach.
While I could theoretically pass the 0th item in the array to my function and shift the remaining entries down, I believe this process would be too performance intensive and not worth the effort. If this approach is actually feasible without significant performance cost, please let me know. However, I am confident that a more effective solution exists.
I'm also open to using other data structures, as the array only contains strings.
Thank you
EDIT: After conducting more research, I had a realization that the issue I've been describing is related to the difference between a stack and a queue. Now, my question is focused on identifying the fastest implementation of a queue when the index is not a priority for me.