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

+20 -15
View File
@@ -391,27 +391,32 @@ export function createKidsScorerStage({
const videoId = typeof video.id === "string" ? video.id : null;
const pubkey = typeof video.pubkey === "string" ? video.pubkey : null;
const positiveComponents = [
{ key: "age-appropriateness", value: ageAppropriateness },
{ key: "educational-boost", value: educationalBoost },
{ key: "author-trust", value: authorTrust },
{ key: "popularity", value: popularityWithinKids },
{ key: "freshness", value: freshness },
];
let maxKey = "age-appropriateness";
let maxValue = ageAppropriateness;
let dominantPositive = positiveComponents[0];
for (const component of positiveComponents) {
if (component.value > dominantPositive.value) {
dominantPositive = component;
}
if (educationalBoost > maxValue) {
maxKey = "educational-boost";
maxValue = educationalBoost;
}
if (authorTrust > maxValue) {
maxKey = "author-trust";
maxValue = authorTrust;
}
if (popularityWithinKids > maxValue) {
maxKey = "popularity";
maxValue = popularityWithinKids;
}
if (freshness > maxValue) {
maxKey = "freshness";
maxValue = freshness;
}
if (dominantPositive.value > 0) {
if (maxValue > 0) {
context?.addWhy?.({
stage: stageName,
type: "score",
reason: dominantPositive.key,
value: dominantPositive.value,
reason: maxKey,
value: maxValue,
score: kidsScore,
videoId,
pubkey,