Skip to content

Commit

Permalink
coerce to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Aug 2, 2024
1 parent 9e292fd commit 7e38a51
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 6 additions & 3 deletions core/computed/tbt-impact-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ class TBTImpactTasks {

tbtImpactTasks.push({
...task,
tbtImpact,
selfTbtImpact,
selfBlockingTime,
// Floating point numbers are not perfectly precise, so the subtraction operations above
// can sometimes output negative numbers close to 0 here. To prevent potentially confusing
// output we should bump those values to 0.
tbtImpact: Math.max(tbtImpact, 0),
selfTbtImpact: Math.max(selfTbtImpact, 0),
selfBlockingTime: Math.max(selfBlockingTime, 0),
});
}

Expand Down
6 changes: 2 additions & 4 deletions core/test/computed/tbt-impact-tasks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ describe('TBTImpactTasks', () => {
};

const tasks = await TBTImpactTasks.request(metricComputationData, context);
// Should be >= 0 but provide some wiggle room for double precision
expect(tasks.every(t => t.selfTbtImpact >= -0.00001)).toBeTruthy();
expect(tasks.every(t => t.selfTbtImpact >= 0)).toBeTruthy();

const tasksImpactingTbt = tasks.filter(t => t.tbtImpact);
expect(tasksImpactingTbt.length).toMatchInlineSnapshot(`7374`);
Expand Down Expand Up @@ -299,8 +298,7 @@ describe('TBTImpactTasks', () => {

const tasks = await TBTImpactTasks.request(metricComputationData, context);

// Should be >= 0 but provide some wiggle room for double precision
expect(tasks.every(t => t.selfTbtImpact >= -0.00001)).toBeTruthy();
expect(tasks.every(t => t.selfTbtImpact >= 0)).toBeTruthy();

const tasksImpactingTbt = tasks.filter(t => t.tbtImpact);
expect(tasksImpactingTbt.length).toMatchInlineSnapshot(`1722`);
Expand Down

0 comments on commit 7e38a51

Please sign in to comment.