sustainable-web-design
v0.0.4
Published
A package for estimating the carbon emissions from using digital services using the Sustainable Web Design model
Downloads
1
Readme
Sustainable Web Design model for Javascript
A library for estimating the carbon emissions from using digital services using the Sustainable Web Design model.
Installation
To begin, you'll need to add the package to your package.json
npm install sustainable-web-design
Usage
import SWD from 'sustainable-web-design';
const results = await SWD.url('https://beleaf.au');
console.log(results);
Use a different model
You can instruct the SWD test method to estimate the emissions using a different model by providing the model number as the third parameter
import SWD from 'sustainable-web-design';
const results = await SWD.url('https://beleaf.au', {}, 1);
console.log(results);
Alternatively, you can access the test method directly on the model and avoid the SWD wrapper
import {V2 as Model} from 'sustainable-web-design';
const results = await Model.url('https://beleaf.au');
console.log(results);
SWD wrapper and Model methods
The SWD wrapper, and each of the SWD models expose 3 methods that can be called to estimate emissions. The models may choose to expose additional methods, but these are not consistent from model to model.
Each of the methods accept atleast 2 parameters, with the SWD wrapper methods accepting a third (integer) representing the model to use. The SWD wrapper defaults to the latest model released.
Each of the models have different formulas for estimating emissions, and each model has different parameters that can be overwritten. If you know the carbon intensity of the electricity grid in which your data center, network, or user devices are located, you should adjust the grid intensity to estimate the operational emissions of that segment. This will give you results that are more representative of how your website is operated and used.
SWD::test
Test will return the estimated emissions for a page view of the url provided. Test requires atleast the url to be tested as the first parameter, and accepts a second parameter allowing for configuration of the test, and the values provided to the formulas.
The test method will make a call to the GPSAPI to calculate the quantity of bytes transferred. The GPSAPI defaults to testing a desktop screens size, but this can be changed to mobile. Additionally, the key can be provided to allow more frequent tests. A key can be acquired from Google.
The test method will also make a call to then Green Web Foundation API's to retrieve energy and emissions values associated with the url provided.
import SWD from 'sustainable-web-design';
const results = await SWD.url('https://beleaf.au', {
'gpsapi': {
'strategy': 'mobile',
'key' => 'xxx',
}
});
console.log(results);
Response
The test method returns an array from the Model::breakdown method. See SWD::breakdown response for the structure.
SWD::breakdown
Breakdown returns an array containing the estimated emissions, the rating (if available on the model - V3 onwards), the estimated emissions for each segment in the model, and the variables utilised by the formulas.
SWD::breakdown accepts two parameters, a int|float representing the bytes transferred, and an array of parameters for configuring the values used in the forumlas.
import SWD from 'sustainable-web-design';
const results = await SWD.breakdown(153735);
console.log(results);
Response
Note: The structure of the reponse differs from model to model.
{
gCO2e: 0.012640330709527517,
rating: 'A+',
segments: {
operationalTotal: 0.00878879241281189,
embodiedTotal: 0.004462495278567076,
operational: {
dataCentre: 0.002491667952085845,
network: 0.0026728801667829974,
userDevice: 0.003624244293943048
},
embodied: {
dataCentre: 0.0005051881447434425,
network: 0.0005472871568053961,
userDevice: 0.0034100199770182374
}
},
variables: {
operationalEnergyIntensity: 531.597,
embodiedEnergyIntensity: 494,
greenHostingFactor: 0.24519999999999997,
dataCacheRatio: 0,
returnVisitorRatio: 0,
gpsapi: { strategy: 'mobile' },
bytes: 91505
}
}
SWD::gCO2e
gCO2e returns a float estimating the emissions.
gCO2e accepts two parameters, a int|float representing the bytes transferred, and an array of parameters for configuring the values used in the forumlas.
import SWD from 'sustainable-web-design';
const results = await SWD.gCO2e(153735);
console.log(results);
Response
0.024419489257414
License
Sustainable Web Design model for PHP is licensed under the MIT License - see the LICENSE file for details.