npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

co2-of-front

v0.3.1

Published

A little add to tgwf/co2 to calculate co2 of front at all stage of the page lifecycle

Downloads

73

Readme

co2-of-front

This library is a use of @tgwf/co2 to mesure the co2 produced by a webpage, directly in the web page. It checks every ressource of the page, and depending on the weight and the domain, determine the co2 produced by it. It also uses some of the greenweb foundation APIs to determine if the server hosting the ressource is green, and what is his gCO2/kWh if its listed. (this is still just a test done by a intern)

Installation

Using NPM

You can install co2-of-front for web projects using NPM :

npm install co2-of-front

Functionning

You have two option to use this library. The first one will give you only the weight and co2 production at the end of the loading of the page. The second one will allow you to have metrics that update everytime a new ressource is loaded into the page. This second option shouldn't be used in a website in production, as it will use more processor, then goes against green practices which is not the goal of this library.

Before using one of the component, you can (and should) change the default options of co2/kWh that will be used if values cannot be found. You can change it by using changeDefaultGridIntensity(newIntensity), with "newIntensity" as it follows :

gridIntensity: {
    device: { country: "FRA" },
    dataCenter: 207,
    network: { country: "FRA" }
}

device and network need to be an object with the code Apha-3 ISO of the country needed, and dataCenter a number.

One time analyse

For mesuring the weight and emissions on the load of the page, you just need to create a new component.

co2Component = new Co2Component();

It will wait for the page to be fully loaded, and then start calculing. The first estimation only uses the total weight of the page, the second estimation, the more accurate one can be a little bit longer to finish.

In case of use in a framework like angular, the metrics car be dynamically use in the component you chose to put it in. ex :

<p>Weight : {{ co2Component.emissions.weight / 1000 }} ko</p>
<p>CO₂ : {{ co2Component.emissions.co2weight }} g</p>

If manually getting the metrics is required, you can use

getCurrectCo2()

To time this call, you'll need to use "currentState". A currentState = 0 means nothing has been calculated. A currentState = 1 means the first estimation is finished. A currentState = 2 means the complexe estimation is finished.

Here is an example of how it could be done :

window.addEventListener("load", async () => {
    await checkForUpdateCo2();
});

async function checkForUpdateCo2() {
    while (co2Component.currentState == 0) {
        await new Promise(resolve => setTimeout(resolve, 100));
    }
    updateCo2();
    while (co2Component.currentState == 1) {
        await new Promise(resolve => setTimeout(resolve, 500));
    }
    updateCo2();

}

function updateCo2() {
    const co2temp = co2Component.getCurrentCo2();
    console.log("Current CO2:", co2temp);
    const emissionsEl = document.querySelector(".emissions");
    if (emissionsEl) {
        emissionsEl.innerHTML = `CO₂: ${co2temp.co2weight} g (Page weight: ${co2temp.weight/1000} Ko)`;
    }
}

Analyse over time

This time, you will need to initialise the component as

co2Component = new ExtendedCo2Component();

For the update to work, a call to

co2Component.doCheck()

You'll need to call this function every X ms, depending on your own preferences. This is the simpliest way in angular.

ngDoCheck() {
    this.co2Component.doCheck();
}