Coder Social home page Coder Social logo

fershad / co2.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thegreenwebfoundation/co2.js

0.0 0.0 0.0 6.83 MB

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

License: Apache License 2.0

JavaScript 100.00%

co2.js's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.