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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-carbon-footprint

v1.0.0

Published

A React hook that monitors network activity and calculates the estimated CO2 emissions based on the Sustainable Web Design model provided by the CO2.js

Downloads

11

Readme

React Carbon Footprint

A React hook that monitors network activity and calculates the estimated CO2 emissions based on the Sustainable Web Design model provided by the CO2.js library. This hook enables developers to measure and optimize the environmental impact of their web applications in real-time.

Installation

To use the carbon footprint hook in your react project, first install the necessary dependencies:

npm install react-carbon-footprint

or

yarn add react-carbon-footprint

Usage

The useCarbonFootprint hook provides a simple interface to monitor network resource loads and estimate the resulting CO2 emissions. Here's a basic example of how to use it in a React component:

import React from 'react';
import { useCarbonFootprint } from 'react-carbon-footprint';

const CarbonFootprintDisplay: React.FC = () => {
  const [gCO2, bytesTransferred] = useCarbonFootprint();

  return (
    <div>
      <h2>Network Usage and CO2 Emissions</h2>
      <p><strong>Bytes Transferred:</strong> {bytesTransferred} bytes</p>
      <p><strong>CO2 Emissions:</strong> {gCO2.toFixed(2)} grams</p>
    </div>
  );
};

export default CarbonFootprintDisplay;

Parameters and Return

The useCarbonFootprint hook returns the following:

  • gramsCO2 (number): The estimated amount of CO2 emitted (in grams) based on the number of bytes transferred over the network.
  • bytesTransferred (number): The total number of bytes transferred over the network since the component was mounted.

🚀 Demo

Explore how the useCarbonFootprint hook works in action!

  • Live demo: Experience the live demo page to see real-time network data tracking and CO2 emissions calculations.

  • Source Code

How It Works

The useCarbonFootprint hook works by monitoring network resource loads in a React component. It calculates the total number of bytes transferred via HTTP requests (such as for images, scripts, and data from APIs) and then estimates the CO2 emissions based on that data.

  1. Network Monitoring:

    • The hook uses the PerformanceObserver API, specifically the PerformanceResourceTiming entries, which track the resources loaded on the page and their associated network metrics, such as transferSize.

    • It calculates the total data transfer in bytes, which includes data such as images, CSS, JavaScript, and API calls made by the application.

  2. CO2 Estimation:

    • The CO2.js library is then used to estimate the carbon emissions from the transferred data. The hook uses the "swd" (Sustainable Web Design) model, which is based on energy usage and carbon intensity from web activities.

Sustainable Web Design CO2 Estimation Model

The Sustainable Web Design model (swd) used in CO2.js provides an estimate of the carbon footprint generated by network data transfers. It calculates the CO2 emissions based on three primary factors:

  • Network energy consumption: The energy used by the network infrastructure (e.g., ISPs, telecom networks).

  • Data center energy consumption: The energy used to store and transmit data from cloud servers and data centers.

  • End-user device consumption: The energy consumed by devices (e.g., smartphones, laptops) used by the user to load the resources.

Each of these components contributes to the total carbon footprint of data transfer, which is measured in grams of CO2 (gCO2).

More details about this estimation model can be found here.

Limitations

The calculation of data transfer size and CO2 emissions relies heavily on browser APIs such as PerformanceObserver and PerformanceResourceTiming. However, there are certain limitations and edge cases to be aware of:

Cross-Origin Resources and Timing-Allow-Origin

One of the key limitations is the handling of cross-origin requests (i.e., requests made to external servers that are not on the same origin as your website).

  • Cross-Origin Resource Timing Restrictions:

    For security and privacy reasons, browsers restrict access to detailed performance metrics for cross-origin resources (e.g., external APIs, images, etc.). This means that for cross-origin requests, the transferSize attribute in PerformanceResourceTiming may be 0, and data transfer cannot be accurately measured unless the server provides the appropriate headers.

  • Timing-Allow-Origin Header:

    If a server does not return the Timing-Allow-Origin header in the response, the browser will not provide detailed timing information (including transferSize) for that resource. This limits the accuracy of the hook's CO2 calculations for cross-origin requests.

    Solution: To ensure accurate tracking of data transfer for cross-origin resources, the server should include the following header in its response:

    Timing-Allow-Origin: *

    This header allows the browser to expose detailed performance metrics for the resource, including the transferSize.

More details about this limitation can be found in the MDN documentation.

License

MIT License

Contributing

Feel free to open issues or submit pull requests to improve this library!