DEV Community

Cover image for Javascript saves the world
Youssef Rabei
Youssef Rabei

Posted on • Updated on

Javascript saves the world

When building apps, we focus on minimizing costs, time to market, bugs, bundle size, application speed, SEO, but we spend almost no effort trying to minimize the damage our applications are making to the environment.

Global-Warming

So Carbon dioxide (CO2) is a greenhouse gas that gets pumped to the atmosphere and acts like a blanket that traps heat.
There is also Methane (CH4) it has 25 times the heating potential of Carbon dioxide.

What is CO2e (Carbon Dioxide Equivalent)

It's 1 ton of Methane, 25 tons of Carbons + some other gases.

Google says it's

The standard unit for measuring carbon footprints. The idea is to express the impact of each different greenhouse gas in terms of the amount of CO2 that would create the same amount of warming.

There was a Paris Climate agreement in 2016 where 187 states agreed on trying to keep the increase in temperature to 2 degrees or ideally 1.5 degrees (We already reached 1 degree)

No one cares about 2 degrees We don't even feel it.
Let me tell you that there are species will extinct if this happened

A report from the UN says that is likely we hit 3.2C by the end of the century

What happens when the climate rise 5Β°C

All water ice on the planet will meltdown, raising the sea levels a few dozen meters above the normal β€œsea levels” and destroying the coastlines, and it will be a catastrophic event obviously, and millions upon millions of animal species (discovered and Undiscovered) will die.

We need to start reducing the climate rise curve

As a Developer you have an impact (A big one too)

Electricity is responsible for 30% of the CO2e.
80% of the world's electricity is created from burning Fossil fuels.
So don't waste electricity, because we destroying our planet to create some electricity so don't wast it.


Microsoft Azure and Google Cloud are Carbon-neutral firms.

Carbon neutral: A product or company that's carbon neutral (or carbon-free) is removing the same amount of carbon dioxide it's emitting into the atmosphere to achieve net-zero carbon emissions, usually by purchasing carbon offsets or credits to make up the difference.


Servers "Carbon" Cost

(The carbon released to create the electricity to run the server)
Let's talk about an absolute Dell server

To run a server

It's about 600 kilograms of carbons that are the electrical cost of it was running at 100% for a year

Idle server

Still costs electricity even nothing running on it, still cost about 25% or 150 kilograms of carbon a year

To create the server

If we said we created that particular server and got four yeara lifespan it produced 320 kilograms of carbon

So idle servers waste almost half a ton of CO2e per year (creation + idleness)


What the hell is serverless?

It's an execution model where the cloud provider (AWS, Azure, or Google Cloud) is responsible for executing a piece of code by dynamically allocating the resources. And only charging for the amount of resources used to run the code.

How serverless help

So when you use something like Microsoft Asure, Google Cloud or AWS your code gets executed next to my code, So we utilize all the servers at 100%, So no waste.

Serverless is greener because

CO2e was released to create the server
CO2e was released to power the server
If you don't run 100% of your server you are wasting CO2e

serverless run servers at 100%


How to go Serverless in Node.js?

We have

  • Microsoft Azure Function
  • AWS Lambda Function
  • Google Cloud Function

So there is a problem when you use azure function you got this

module.exports = async function(context, req) {
  context.log("Javascript HTTP trigger function processed a request.");

  if (req.query.name || (req.body && req.body.name)) {
    context.res = {
      // status: 200, /* Defaults to 200 */
      body: "Hello " + (req.query.name || req.body.name)
    };
  } else {
    context.res = {
      status: 400,
      body: "Please pass a name on the query string or in the request body"
    };
  }
};
Enter fullscreen mode Exit fullscreen mode

So we gonna use nestjs

with Microsoft Azure

nest add @nestjs/azure-func-http
Enter fullscreen mode Exit fullscreen mode

Basically, it adds or updates 10 files into your nest project to serverless application
And when you want to deploy (I recommend to) use a tool named Hexa

npm install -g @manekinekko/hexa
hexa init
hexa deploy
Enter fullscreen mode Exit fullscreen mode

And you are good to go

To know more about nestjs with

I'm gonna write about serverless and nodejs again but tech-wise only, with AWS, Google and Azure


Credits

Photo by Ciprian Morar on Unsplash


Thanks for reading

Top comments (0)