AI driven open source library for measuring your web page carbon footprint
Photo by Chris LeBoutillier
Some people say that 2023 will be the year of AI. With tools like ChatGPT and DALL·E the world is definitely changing. There are different opinions on how such instruments will affect the everyday tasks of the professionals. My two cents (for now) are that those are just tools to achieve what we want quicker and more efficiently. Who knows, someday I may call myself a "prompt engineer". For the time being, though, I'm still writing a lot of stuff alone and occasionally ask ChatGPT for help.
Backstory
So, what is this article all about? Friday evening, reading Twitter, the weekend is approaching, and I'm wondering if I have to work on a small project. You know, hacking something quickly for fun. I had no ideas, so I decided to ask, guess who - ChatGPT:
Are you wondering what to do over the weekend? No worries ... ChatGPT got you covered. #chatgpt #javascript pic.twitter.com/j73oT5viuu
— Krasimir (@KrasimirTsonev) January 13, 2023
Challenge accepted! A good friend of mine (and colleague) Lorenzo Pieri did an interesting talk some time ago called "Greta is not happy with your website, and she’s right.". I never thought about my job from that angle, and I'm finding the idea of measuring the carbon footprint interesting. He even assembled the website envirotechnical.eu, where we can find materials on that same topic.
Implementation
The idea is in place, and I decided to continue using ChatGPT. The first and very significant "problem" is the library's name. Well, eco.js is taken so:
Boom! It will be Carboscope. Job done 😀. Now we have a name; let's see how to write this library. I did a quick research, and it is not simple. There are companies specializing in such types of measurement, and implementing something accurate for just one weekend is not achievable. But again, let's ask the bot:
I tried using the battery API, but that's only giving me the percentage to the second number after the dot. So, I'm not sure I can make meaningful conclusions based on that. I like the second point.
Ok, ok, I get it. It's complicated. But in that last answer, something is interesting. The third point talks about performance.memory. I decided to see what's this about, and it looks like I can get the heap size of the currently running JavaScript. I could correlate this to a formula where I used the memory usage against the consumed energy.
Sadly, performance.memory is deprecated. So, that's a dead end.
A-ha ... performance.getEntries() is what I'm going to use. That's not deprecated. According to MDN there is a transferSize property that represents the size (in octets) of the fetched resource. This may not include the memory used by JavaScript, but it contains the downloaded files.
A couple of minutes later and I had this:
function _getMemory() {
if (typeof performance !== 'undefined' && performance.getEntries) {
let size = performance.getEntries().reduce(
(total, entry) => total + entry.transferSize || 0,
0
);
return size / (1024 * 1024); // in MB
} else {
console.warn('Performance API is not supported');
return 0;
}
};
This function tells me how many MBs the current page consumed (kind of). Now I need the formula to calculate the carbon footprint.
Fast forward to Sunday when I'm writing this article. I used the formula above and published a 660B library called Carboscope.
The library
Carboscope is basically a short JavaScript snippet. Get the code from here, paste it into the console of any page, and run CarboScope.measure(). You'll start getting messages about the carbon footprint. For example:
🌿 CarboScope: 5.23 kg CO2e
🌿 CarboScope: 5.41 kg CO2e
🌿 CarboScope: 8.22 kg CO2e
The library is also distributed as an npm package if you need, for some reason, to get the footprint as a number.
// npm install carboscope and then:
import CarboScope from 'carboscope';
CarboScope.asNumber();
Conclusion
It was fun 🤓. ChatGPT proved to be a valuable asset during my brainstorming session. Not only did it provide insightful information on various topics, but it also effectively guided me towards a solution for my problem. While it's important to note that the suggested solutions may not always be the most optimal, I was still impressed with its usefulness and capabilities.