Optimize object allocation in kids scoring loop

- Replaced array allocation and loop with unrolled scalar comparisons in `kidsScorerStage`.
- Added unit tests and benchmark.
- Measured ~12% performance improvement (282ms -> 249ms for 200k items).

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-08 03:41:50 +00:00
1 parent 1fbee4219b
commit 96dea42692
3 files changed
+156 -15

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createKidsScorerStage } from "../../js/feedEngine/kidsScoring.js";
test("kids scorer correctly identifies dominant positive component", async () => {
const stage = createKidsScorerStage({
weights: {
w_age: 0.2,
w_edu: 0.2,
w_author: 0.2,
w_pop: 0.2,
w_fresh: 0.2,
w_risk: 0
}
});
// Create an item where one component is clearly dominant
// Age appropriateness: 1.0 (perfect duration + tags)
// Others: 0
const items = [
{
video: {
id: "v1",
pubkey: "pk1",
duration: 300, // 5 min (perfect for toddler/preschool)
tags: [["t", "preschool"], ["t", "learning"]], // Matches preferred
created_at: Date.now() / 1000 - 10000000, // Old
viewCount: 0
},
metadata: {},
},
];
const why = [];
const context = {
runtime: {
ageGroup: "preschool",
trustedAuthors: []
},
addWhy: (detail) => why.push(detail)
};
await stage(items, context);
assert.equal(why.length, 1);
assert.equal(why[0].reason, "age-appropriateness");
assert.ok(why[0].value > 0);
});
test("kids scorer picks educational-boost when dominant", async () => {
const stage = createKidsScorerStage({
educationalTags: ["math"],
weights: { w_age: 0, w_edu: 1, w_author: 0, w_pop: 0, w_fresh: 0, w_risk: 0 }
});
const items = [{
video: {
id: "v2",
tags: [["t", "math"]],
duration: 300
},
metadata: {}
}];
const why = [];
await stage(items, {
runtime: { educationalTags: ["math"] },
addWhy: (d) => why.push(d)
});
assert.equal(why.length, 1);
assert.equal(why[0].reason, "educational-boost");
});