Backporting a bunch of optimizations from db-only branch
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { PromiseQueue, sleep } from "./async.ts";
|
||||
import { assert, assertEquals } from "../../test_deps.ts";
|
||||
import { batchRequests, PromiseQueue, sleep } from "./async.ts";
|
||||
|
||||
Deno.test("PromiseQueue test", async () => {
|
||||
const q = new PromiseQueue();
|
||||
@@ -24,3 +24,19 @@ Deno.test("PromiseQueue test", async () => {
|
||||
});
|
||||
assertEquals(wasRun, true);
|
||||
});
|
||||
|
||||
Deno.test("Batch test", async () => {
|
||||
// Generate an array with numbers up to 100
|
||||
const elements = Array.from(Array(100).keys());
|
||||
const multiplied = await batchRequests(elements, async (batch) => {
|
||||
await sleep(2);
|
||||
// Batches should be 9 or smaller (last batch will be smaller)
|
||||
assert(batch.length <= 9);
|
||||
return batch.map((e) => e * 2);
|
||||
}, 9);
|
||||
assertEquals(multiplied, elements.map((e) => e * 2));
|
||||
const multiplied2 = await batchRequests(elements, async (batch) => {
|
||||
return batch.map((e) => e * 2);
|
||||
}, 10000);
|
||||
assertEquals(multiplied2, elements.map((e) => e * 2));
|
||||
});
|
||||
|
||||
@@ -67,3 +67,25 @@ export class PromiseQueue {
|
||||
this.run(); // Continue processing the next promise in the queue
|
||||
}
|
||||
}
|
||||
|
||||
export async function batchRequests<I, O>(
|
||||
values: I[],
|
||||
fn: (batch: I[]) => Promise<O[]>,
|
||||
batchSize: number,
|
||||
): Promise<O[]> {
|
||||
const results: O[] = [];
|
||||
// Split values into batches of batchSize
|
||||
const batches: I[][] = [];
|
||||
for (let i = 0; i < values.length; i += batchSize) {
|
||||
batches.push(values.slice(i, i + batchSize));
|
||||
}
|
||||
// Run fn on them in parallel
|
||||
const batchResults = await Promise.all(batches.map(fn));
|
||||
// Flatten the results
|
||||
for (const batchResult of batchResults) {
|
||||
if (Array.isArray(batchResult)) { // If fn returns an array, collect them
|
||||
results.push(...batchResult);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user