Backporting a bunch of optimizations from db-only branch

This commit is contained in:
Zef Hemel
2024-01-13 17:30:15 +01:00
parent 509ece91f0
commit bf1eb03129
24 changed files with 264 additions and 104 deletions
+18 -2
View File
@@ -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));
});