| test name | time taken (ms) | executions per sec | sample deviation |
|---|---|---|---|
| 1,000,000 push | 14.55 | 68.72 | 6.91e-4 |
| 1,000,000 push & pop | 23.40 | 42.73 | 5.94e-4 |
| 1,000,000 push & shift | 24.41 | 40.97 | 1.45e-4 |
| 1,000,000 unshift & shift | 22.56 | 44.32 | 1.30e-4 |
Deque
npm install deque-typed!NPM
!GitHub top language
!npm
!eslint
!npm bundle size
!npm bundle size
!npm
This is a standalone Deque data structure from the data-structure-typed collection. If you wish to access more data
structures or advanced features, you can transition to directly installing the
complete data-structure-typed package
``bash`
npm i deque-typed --save
`bash`
yarn add deque-typed
[//]: # (No deletion!!! Start of Example Replace Section)
typescript
// Create a simple Deque with initial values
const deque = new Deque([1, 2, 3, 4, 5]); // Verify the deque maintains insertion order
console.log([...deque]); // [1, 2, 3, 4, 5];
// Check length
console.log(deque.length); // 5;
// Push to the end
deque.push(6);
console.log(deque.length); // 6;
// Pop from the end
const last = deque.pop();
console.log(last); // 6;
`$3
`typescript
const deque = new Deque([20, 30, 40]); // Unshift adds to the front
deque.unshift(10);
console.log([...deque]); // [10, 20, 30, 40];
// Shift removes from the front (O(1) complexity!)
const first = deque.shift();
console.log(first); // 10;
// Verify remaining elements
console.log([...deque]); // [20, 30, 40];
console.log(deque.length); // 3;
`$3
`typescript
const deque = new Deque([10, 20, 30, 40, 50]); // Get first element without removing
const first = deque.at(0);
console.log(first); // 10;
// Get last element without removing
const last = deque.at(deque.length - 1);
console.log(last); // 50;
// Length unchanged
console.log(deque.length); // 5;
`$3
`typescript
const deque = new Deque(['A', 'B', 'C', 'D']); // Iterate forward
const forward: string[] = [];
for (const item of deque) {
forward.push(item);
}
console.log(forward); // ['A', 'B', 'C', 'D'];
// Reverse the deque
deque.reverse();
const backward: string[] = [];
for (const item of deque) {
backward.push(item);
}
console.log(backward); // ['D', 'C', 'B', 'A'];
`$3
`typescript
interface DataPoint {
timestamp: number;
value: number;
sensor: string;
} // Create a deque-based sliding window for real-time data aggregation
const windowSize = 3;
const dataWindow = new Deque();
// Simulate incoming sensor data stream
const incomingData: DataPoint[] = [
{ timestamp: 1000, value: 25.5, sensor: 'temp-01' },
{ timestamp: 1100, value: 26.2, sensor: 'temp-01' },
{ timestamp: 1200, value: 25.8, sensor: 'temp-01' },
{ timestamp: 1300, value: 27.1, sensor: 'temp-01' },
{ timestamp: 1400, value: 26.9, sensor: 'temp-01' }
];
const windowResults: Array<{ avgValue: number; windowSize: number }> = [];
for (const dataPoint of incomingData) {
// Add new data to the end
dataWindow.push(dataPoint);
// Remove oldest data when window exceeds size (O(1) from front)
if (dataWindow.length > windowSize) {
dataWindow.shift();
}
// Calculate average of current window
let sum = 0;
for (const point of dataWindow) {
sum += point.value;
}
const avg = sum / dataWindow.length;
windowResults.push({
avgValue: Math.round(avg * 10) / 10,
windowSize: dataWindow.length
});
}
// Verify sliding window behavior
console.log(windowResults.length); // 5;
console.log(windowResults[0].windowSize); // 1; // First window has 1 element
console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
console.log(dataWindow.length); // 3;
``[//]: # (No deletion!!! End of Example Replace Section)
| Data Structure | Unit Test | Performance Test | API Docs |
|---|---|---|---|
| Deque | Deque |
| Data Structure Typed | C++ STL | java.util | Python collections |
|---|---|---|---|
| Deque<E> | deque<T> | ArrayDeque<E> | deque |
[//]: # (No deletion!!! Start of Replace Section)
| test name | time taken (ms) | executions per sec | sample deviation |
|---|---|---|---|
| 1,000,000 push | 14.55 | 68.72 | 6.91e-4 |
| 1,000,000 push & pop | 23.40 | 42.73 | 5.94e-4 |
| 1,000,000 push & shift | 24.41 | 40.97 | 1.45e-4 |
| 1,000,000 unshift & shift | 22.56 | 44.32 | 1.30e-4 |
[//]: # (No deletion!!! End of Replace Section)
| Algorithm | Function Description | Iteration Type |
|---|
| Principle | Description |
|---|---|
| Practicality | Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names. |
| Extensibility | Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures. |
| Modularization | Includes data structure modularization and independent NPM packages. |
| Efficiency | All methods provide time and space complexity, comparable to native JS performance. |
| Maintainability | Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns. |
| Testability | Automated and customized unit testing, performance testing, and integration testing. |
| Portability | Plans for porting to Java, Python, and C++, currently achieved to 80%. |
| Reusability | Fully decoupled, minimized side effects, and adheres to OOP. |
| Security | Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects. |
| Scalability | Data structure software does not involve load issues. |