CO2.js exposes two functions that developers can use to return CO2 estimates. This guide will show you how to use the different methods to calculate carbon emissions, and give a brief explanation for each.
The perByte()
function can be used with both the OneByte and Sustainable Web Design models. We recommend using this function, as it will return a CO2 value (in grams) for raw data transfer using a given model. It can be used for calculating emissions from websites, file uploads, streaming etc.
import { co2 } from "@tgwf/co2";
const oneByte = new co2({ model: "1byte" });
const emissions = oneByte.perByte(1000000);
Here we are using the OneByte model, and using the perByte()
function to check the carbon emissions for transferring 1 million bytes.
The perByte()
function accepts the following parameters:
number
boolean
Optional: if the data being measured is served from a green web host.The perByte()
function returns a floating point decimal value which is the amount of CO2e (in grams) calculated using the function.
The perByteTrace()
function is an extension of perByte()
which allows for certain inputs to be adjusted. This allows for more accurate, scenario specific estimates to be produced using the Sustainable Web Design model.
The perByteTrace()
function accepts the following parameters:
number
boolean
Optional: if the data being measured is served from a green web host.object
Optional - A JavaScript object containing any Sustainable Web Design specific variables to be change.options
parameterThe options
parameter can contain any of the following keys. These can be used to adjust the values used by the Sustainable Web Design model's calculation.
gridIntensity
Optional – an object
that can contain the following keys:
device
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.dataCenter
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.networks
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.Below is an example which shows both kinds of accepted inputs in use.
const options = {
gridIntensity: {
device: 565.629, // Here we have set the grid intensity at the device location using a number.
dataCenter: { country: "TWN" }, // Here we have set the data center grid intensity using a country code.
networks: 442,
},
};
The perByteTrace()
function returns an object with the following keys:
co2
- A number
representing the carbon emissions calculated in gramsgreen
- A boolean
indicating whether the calculation was based on data being hosted in a green data centervariables
- An `object`` that details all the other variables used in the calculation. This object will include:
description
- A static string
explaining what is being shown.bytes
- A number
showing the bytes that were passed into the function.gridIntensity
– an object
that can contain the following keys:
device
– A number
representing the carbon intensity (in grams per kilowatt-hour) used in the calculation.dataCenter
– A number
representing the carbon intensity (in grams per kilowatt-hour) used in the calculation.networks
– A number
representing the carbon intensity (in grams per kilowatt-hour) used in the calculation.The perVisit()
function can only be used with the Sustainable Web Design models. This function includes assumptions the model authors have made about website visitors and caching as part its calculation. For that reason, we recommend only using it if you are comfortable with those assumptions. The perVisit()
function is best used for calculating website carbon emissions.
import { co2 } from "@tgwf/co2";
const swd = new co2({ model: "swd" });
const emissions = swd.perVisit(1000000);
Here we are using the Sustainable Web Design model, and using the perVisit()
function to check the carbon emissions for transferring 1 million bytes.
The perVisit()
function accepts the following parameters:
number
boolean
Optional: if the data being measured is served from a green web host.The perVisitTrace()
function is an extension of perVisit()
which allows for certain inputs to be adjusted. This allows for more accurate, scenario specific estimates to be produced using the Sustainable Web Design model.
The perVisitTrace()
function accepts the following parameters:
number
boolean
Optional: if the data being measured is served from a green web host.object
Optional - A JavaScript object containing any Sustainable Web Design specific variables to be change.options
parameterThe options
parameter can contain any of the following keys. These can be used to adjust the values used by the Sustainable Web Design model's calculation.
dataReloadRatio
Optional – a number
between 0 and 1 representing the percentage of data that is downloaded by return visitors.firstVisitPercentage
Optional – a number
between 0 and 1 representing the percentage of new visitors.returnVisitPercentage
Optional – a number
between 0 and 1 representing the percentage of returning visitors.gridIntensity
Optional – an object
that can contain the following keys:
device
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.dataCenter
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.networks
Optional – A number
representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object
, which contains a key of country and a value that is an Alpha-3 ISO country code.Below is an example which shows both kinds of accepted inputs in use.
const options = {
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
gridIntensity: {
device: 565.629, // Here we have set the grid intensity at the device location using a number.
dataCenter: { country: "TWN" }, // Here we have set the data center grid intensity using a country code.
networks: 442,
},
};
The perByteTrace()
function returns an object with the following keys:
co2
- A number
representing the carbon emissions calculated in gramsgreen
- A boolean
indicating whether the calculation was based on data being hosted in a green data centervariables
- An `object`` that details all the other variables used in the calculation. This object will include:
description
- A static string
explaining what is being shown.bytes
- A number
showing the bytes that were passed into the function.gridIntensity
– an object
that can contain the following keys:
device
– A number
representing the carbon intensity for this segment (in grams per kilowatt-hour) used in the calculation.dataCenter
– A number
representing the carbon intensity for this segment (in grams per kilowatt-hour) used in the calculation.networks
– A number
representing the carbon intensity for this segment (in grams per kilowatt-hour) used in the calculation.production
- A number
representing the carbon intensity for this segment (in grams per kilowatt-hour) used in the calculation.dataReloadRatio
– a number
between 0 and 1 representing the percentage of data that is downloaded by return visitors.firstVisitPercentage
– a number
between 0 and 1 representing the percentage of new visitors.returnVisitPercentage
– a number
between 0 and 1 representing the percentage of returning visitors.