Skip to content

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services

License

Notifications You must be signed in to change notification settings

Environmental-Predictions/co2.js

 
 

Repository files navigation

CO2

We know computers use electricity, and because most of the electricity we use comes from burning fossil fuels, there is an environmental cost to every upload and download we make over the internet.

We can do something about this though. The same way we use performance budgets to make apps and websites faster and cheaper to run, we can use carbon budgets to make them faster, cheaper and greener.

The CO2 package from The Green Web Foundation lets you quickly estimate these emissions, to make measurable improvements as part of your workflow.

How it works

It does this by implementing various models for converting the measurable usage of digital services into a figure for the estimated CO2 emissions.

This defaults to the 1byte model as used by the Shift Project, as introduced in their report on CO2 emissions from digital infrastructure, Lean ICT: for a sober digital, with the Sustainable Web Design model as a popular alternative..

For more information, see the documentation for when to use the different models, with code samples to start you off.

Who uses it

It is currently used in the web performance tool sitespeed.io, ecoping, Websitecarbon.com, and ecograder to help developers build greener, more planet friendly digital services.

If you want to build this kind of environmental information into your own software, and want some advice, we'd be happy to hear from you - please open an issue, remembering to link to your project.

This is open source software, with all the guarantees associated. So if you want professional advice, to a deadline, and you have a budget please see the services offered by the Green Web Foundation.

Usage

Calculating emissions per byte

Server-side

This approach relies on the fs module and so can only be used on platforms like Node.js, that support this.

const CO2 = require('@tgwf/co2')
const bytesSent = (1024 * 1024 * 1024)
const co2Emission = new CO2();
estimatedCO2 = co2Emission.perByte(bytesSent)

console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)

Browser-side

For browser-based solutions, you must import the co2.js submodule directly from node_modules. For example, like this:

const CO2 = require('node_modules/@tgwf/co2/src/co2.js')
const bytesSent = (1024 * 1024 * 1024)
const co2Emission = new CO2();
estimatedCO2 = co2Emission.perByte(bytesSent)

console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
****

Checking for green power

Because different digital services and websites use different forms of power, there is also a module for checking if a domain uses green power or not, and whether the domains linked to on a page use green power as well.

const greencheck = require('@tgwf/hosting')

// returns true if green, otherwise false
greencheck.check("google.com")

// returns an array of the green domains, in this case ["google.com"].
greencheck.check(["google.com", "kochindustries.com"])]

// returns an array of green domains, again in this case, ["google.com"]
greencheck.checkPage(["google.com"])

Licenses

Apache 2.0

About

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%