You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-15Lines changed: 27 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,16 @@
2
2
3
3
The `SlimQueue` class implements an in-memory queue with a basic API, targeting pure FIFO use cases such as task queues, breadth-first search (BFS), and similar scenarios.
4
4
5
+
This versatile data structure is commonly associated with task prioritization, ensuring tasks are processed in order. It also proves valuable in optimizing sliding-window algorithms, where the oldest item (First In) is evicted based on a specific indicator.
6
+
5
7
## Data-Oriented Design :gear:
6
8
7
9
This implementation follows the principles of Data-Oriented Design (DOD), optimizing memory layout and access patterns using arrays, particularly to enhance CPU cache efficiency. Unlike Object-Oriented Programming (OOP), where each object may be allocated in disparate locations on the heap, DOD leverages the sequential allocation of arrays, reducing the likelihood of cache misses.
8
10
9
11
## Focused API :dart:
10
12
11
-
This package provides a queue and nothing more. The absence of linear operations like iteration and splicing reflects a deliberate design choice, as resorting to such methods often indicates that a queue may not have been the most appropriate data structure in the first place.
13
+
This package provides a queue and nothing more. The absence of linear operations like iteration and splicing reflects a deliberate design choice, as resorting to such methods often indicates that a queue may not have been the most appropriate data structure in the first place.
14
+
The sole exception is the `getSnapshot` method, included to support metrics or statistical analysis, particularly in use cases like tracking the K most recent items.
12
15
13
16
## Table of Contents :bookmark_tabs:
14
17
@@ -21,10 +24,10 @@ This package provides a queue and nothing more. The absence of linear operations
21
24
22
25
## Key Features :sparkles:<aid="key-features"></a>
23
26
24
-
-__Basic Queue API__: Straightforward API, targeting pure use-cases of queues.
27
+
-__Basic Queue API__: Targeting pure use-cases of queues.
25
28
-__Efficiency :gear:__: Featuring a Data-Oriented Design with capacity-tuning capability, to reduce or prevent reallocations of the internal cyclic buffer.
26
29
-__Comprehensive Documentation :books:__: The class is thoroughly documented, enabling IDEs to provide helpful tooltips that enhance the coding experience.
27
-
-__Tests :test_tube:__: **Fully covered** by comprehensive unit tests, including validations to ensure that internal capacity increases as expected.
30
+
-__Tests :test_tube:__: **Fully covered** by comprehensive unit tests, including **randomized simulations** of real-life scenarios and validations to ensure proper internal capacity scaling.
28
31
-**TypeScript** support.
29
32
- No external runtime dependencies: Only development dependencies are used.
30
33
- ES2020 Compatibility: The `tsconfig` target is set to ES2020, ensuring compatibility with ES2020 environments.
@@ -33,9 +36,10 @@ This package provides a queue and nothing more. The absence of linear operations
33
36
34
37
The `SlimQueue` class provides the following methods:
35
38
36
-
*__push__: Appends the item to the end of the queue as the "Last In" item. As a result, the queue's size increases by one.
37
-
*__pop__: Returns the oldest item currently stored in the queue and removes it. As a result, the queue's size decreases by one.
38
-
*__clear__: Removes all items from the current queue instance, leaving it empty.
39
+
*__push__: Appends an item to the end of the queue (i.e., the Last In), increasing its size by one.
40
+
*__pop__: Removes and returns the oldest (First In) item from the queue, decreasing its size by one.
41
+
*__clear__: Removes all items from the queue, leaving it empty.
42
+
*__getSnapshot__: Returns an array of references to all the currently stored items in the queue, ordered from First-In to Last-In.
39
43
40
44
If needed, refer to the code documentation for a more comprehensive description.
41
45
@@ -47,15 +51,16 @@ The `SlimQueue` class provides the following getter methods to reflect the curre
47
51
48
52
*__size__: The amount of items currently stored in the queue.
49
53
*__isEmpty__: Indicates whether the queue does not contain any item.
54
+
*__firstIn__: Returns a reference to the oldest item currently stored in the queue (the First In item), which will be removed during the next pop operation.
50
55
*__capacity__: The length of the internal buffer storing items. If the observed capacity remains significantly larger than the queue's size after the initial warm-up period, it may indicate that the initial capacity was overestimated. Conversely, if the capacity has grown excessively due to buffer reallocations, it may suggest that the initial capacity was underestimated.
51
56
*__numberOfCapacityIncrements__: The number of internal buffer reallocations due to insufficient capacity that have occurred during the instance's lifespan. A high number of capacity increments suggests that the initial capacity was underestimated.
52
-
*__firstIn__: The oldest item currently stored in the queue, i.e., the "First In" item, which will be removed during the next pop operation.
53
57
54
58
To eliminate any ambiguity, all getter methods have **O(1)** time and space complexity.
55
59
56
60
## Use Case Example: Rate Limiting :man_technologist:<aid="use-case-example"></a>
57
61
58
-
Consider a component designed for rate-limiting promises using a sliding-window approach. Suppose a window duration of `windowDurationMs` milliseconds, with a maximum of `tasksPerWindow` tasks allowed within each window. The rate limiter will only trigger the execution of a task if fewer than `tasksPerWindow` tasks have started execution within the time window `[now - windowDurationMs, now]`.
62
+
Consider a component designed for rate-limiting promises using a sliding-window approach. Suppose a window duration of `windowDurationMs` milliseconds, with a maximum of `tasksPerWindow` tasks allowed within each window. The rate limiter will only trigger the execution of a task if fewer than `tasksPerWindow` tasks have started execution within the time window `[now - windowDurationMs, now]`.
63
+
59
64
For simplicity, this example focuses on a single method that initiates task execution only if the current window's limit has not been reached. If the limit has been exceeded, an error is thrown.
60
65
In this scenario, we employ the `isEmpty`, `firstIn`, and `size` getters, along with the `push` and `pop` methods.
0 commit comments