mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-29 17:02:28 +00:00
Merge pull request #2125 from PR0M3TH3AN/perf/optimize-feed-engine-normalization-10953414898380924451
⚡ Optimize Feed Engine Normalization Loop
This commit is contained in:
2 files changed
+77
-3
No files matched your search
@@ -0,0 +1,76 @@
|
||||
|
||||
import { createFeedEngine } from "../js/feedEngine/index.js";
|
||||
import { performance } from "perf_hooks";
|
||||
|
||||
// Mock logger
|
||||
const logger = {
|
||||
log: () => {},
|
||||
warn: () => {},
|
||||
error: () => {},
|
||||
};
|
||||
|
||||
// Polyfill localStorage to silence errors
|
||||
if (typeof globalThis.localStorage === "undefined") {
|
||||
globalThis.localStorage = {
|
||||
getItem: () => null,
|
||||
setItem: () => {},
|
||||
removeItem: () => {},
|
||||
};
|
||||
}
|
||||
|
||||
async function runBenchmark() {
|
||||
const engine = createFeedEngine({ logger });
|
||||
const feedName = "bench-feed";
|
||||
|
||||
// Create items
|
||||
const itemCount = 100000;
|
||||
const items = [];
|
||||
for (let i = 0; i < itemCount; i++) {
|
||||
items.push({
|
||||
video: { id: `v${i}`, created_at: Date.now() },
|
||||
metadata: { some: "data" },
|
||||
});
|
||||
}
|
||||
|
||||
// Define stages
|
||||
const stages = [];
|
||||
|
||||
// 10 stages that return null (no change) - STRICT PASS THROUGH
|
||||
for (let i = 0; i < 10; i++) {
|
||||
stages.push(async (items) => {
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
// 10 stages that return the same items array (identity) - STRICT PASS THROUGH
|
||||
for (let i = 0; i < 10; i++) {
|
||||
stages.push(async (items) => {
|
||||
return items;
|
||||
});
|
||||
}
|
||||
|
||||
engine.registerFeed(feedName, {
|
||||
source: async () => [...items],
|
||||
stages: stages,
|
||||
});
|
||||
|
||||
console.log(`Starting benchmark with ${itemCount} items and ${stages.length} stages...`);
|
||||
|
||||
const iterations = 50;
|
||||
const start = performance.now();
|
||||
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
await engine.runFeed(feedName);
|
||||
if (i % 5 === 0) process.stdout.write(".");
|
||||
}
|
||||
|
||||
const end = performance.now();
|
||||
const totalTime = end - start;
|
||||
const avgTime = totalTime / iterations;
|
||||
|
||||
console.log("\n");
|
||||
console.log(`Total time: ${totalTime.toFixed(2)}ms`);
|
||||
console.log(`Average time per run: ${avgTime.toFixed(2)}ms`);
|
||||
}
|
||||
|
||||
runBenchmark().catch(console.error);
|
||||
@@ -205,10 +205,8 @@ export function createFeedEngine({ logger } = {}) {
|
||||
|
||||
for (const stage of entry.stages) {
|
||||
const result = await stage(items, context);
|
||||
if (Array.isArray(result)) {
|
||||
if (Array.isArray(result) && result !== items) {
|
||||
items = normalizeItems(result);
|
||||
} else if (result == null) {
|
||||
items = normalizeItems(items);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user