Skip to content

Commit fb76408

Browse files
committed
feat: calculate emissions from page speed results
1 parent e5599e6 commit fb76408

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"node": ">=14.0.0"
2323
},
2424
"dependencies": {
25+
"@tgwf/co2": "^0.16.9",
2526
"archiver": "^5.3.1"
26-
},
27-
"devDependencies": {}
28-
}
27+
}
28+
}

src/core/CarbonCalculator.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { co2 } from "@tgwf/co2";
2+
3+
/**
4+
* Calculates the carbon emissions for a web page based on the results from Google's PageSpeed Insights (Lighthouse).
5+
*
6+
* @param {Object} pageSpeedResults - The PageSpeed Insights (Lighthouse) result object.
7+
* @returns {Object} An object containing:
8+
* @property {number} emissionsOneByte - Estimated emissions CO2e (in grams) from the OneByte model.
9+
* @property {number} emissionsSWD - Estimated emissions CO2e (in grams) from the Sustainable Web Design model.
10+
* TODO: This function currently returns emissions for both the OneByte and SWD models. Not sure which one we ultimately want to use.
11+
*/
12+
export const calculateEmissionsFromPageSpeedResults = (pageSpeedResults) => {
13+
const totalByteWeight = pageSpeedResults.lighthouseResult.audits['total-byte-weight'].numericValue;
14+
15+
// Sustainable Web Design (SWD) model
16+
// https://developers.thegreenwebfoundation.org/co2js/models/#using-the-sustainable-web-design-model-default-v0110
17+
const swd = new co2({ model: "swd", version: 4 });
18+
const emissionsPerByte = swd.perByte(totalByteWeight);
19+
20+
return { bytesTransferred: totalByteWeight, totalCO2: emissionsPerByte };
21+
}

0 commit comments

Comments
 (0)