Helping the Environment by Saving Two Centuries of Compute time
Tags: tooling, javascript, environment, monorepo
Published: 2022-08-18 15:48:12

---
title: Helping the Environment by Saving Two Centuries of Compute time
published: true
description: How Nx & Nx Cloud computation caching help the environment
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgx1wrourqe5ki88b8uy.png
canonical_url: https://blog.nrwl.io/helping-the-environment-by-saving-two-centuries-of-compute-time-feea8e1ce22?source=friends_link&sk=9b1259d0b171a7b95ebe95b3795660b5
tags:
- tooling
- javascript
- environment
- monorepo
---

Among the core features of Nx is the ability to save computation time by applying different strategies. Scroll to the end of the article for some more info, but first, **how much time is actually being saved?**

## How much time is being saved?

This is how much got saved so far (data from August 16th, 2022). Pretty crazy!

![Compute time saved by Nx Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rb0ylq9ifrtba4b0or6.png)

Here are the raw numbers:

- **Last 7 days:** 5 years 4 months 2 days 2 hours 32 minutes 46 seconds
- **Last 30 days:** 23 years 8 months 25 days 8 hours 57 minutes 19 seconds
- **Since beginning of Nx Cloud:** 200 years 10 months 13 days 19 hours 37 minutes 57 seconds

We're soon having a page on [nx.app](https://nx.app) where you can inspect the live numbers as they keep growing in a lightning-fast manner!! (no wonder, [Nx is about to hit 2.5 million downloads / week](https://twitter.com/NxDevTools/status/1559540784730292224?s=20&t=lawBciDmuc4ojkz_OZNmlQ))

## The Effect on the Environment

Calculating the CO2 emissions can be tricky. It really depends on what machines are being used to run the computation saved by Nx Cloud. We gave it a try by using https://green-algorithms.org/.

Here are some impressive savings 🤯.

**Last 7-day savings correspond to:**

![CO2 emission savings for the last 7 days thanks to Nx Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsp48wn16h5sjrch7xd1.png)

[See all the details](https://green-algorithms.org//?runTime_hour=46752&runTime_min=0&appVersion=v2.2&locationContinent=North%20America&locationCountry=United%20States%20of%20America&locationRegion=US&coreType=CPU&numberCPUs=2&CPUmodel=Xeon%20E5-2683%20v4&memory=4&platformType=cloudComputing&provider=aws)

**Last 30-day savings correspond to:**

![CO2 emission savings for the last 30 days thanks to Nx Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k6i35b72sclbirux0xl.png)

[See all the details](https://green-algorithms.org//?runTime_hour=207462&runTime_min=0&appVersion=v2.2&locationContinent=North%20America&locationCountry=United%20States%20of%20America&locationRegion=US&coreType=CPU&numberCPUs=2&CPUmodel=Xeon%20E5-2683%20v4&memory=4&platformType=cloudComputing&provider=aws)

**Since beginning of Nx Cloud:**

![CO2 emission savings since the beginning of Nx Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n222903pxjs54fudhuwr.png)

[See all the details](https://green-algorithms.org//?runTime_hour=1760505&runTime_min=0&appVersion=v2.2&locationContinent=North%20America&locationCountry=United%20States%20of%20America&locationRegion=US&coreType=CPU&numberCPUs=2&CPUmodel=Xeon%20E5-2683%20v4&memory=4&platformType=cloudComputing&provider=aws)

## Help me out! A Primer on how Nx saves computation

Nx has various strategies to help you reduce computation time, locally and on CI. Here's a very short overview of the strategies Nx applies with some links for further reading.

### Affected Commands

Example: Run tests only for changed projects in a given PR.

```
nx affected:test
```

[Nx affected commands](https://nx.dev/using-nx/affected) allow you to only run commands against projects that changed with respect to a baseline. Usually, this is applied in PRs processed by your CI system. Nx analyzes the Git commits and identifies all projects that got changed with respect to a base branch (usually `main` or `master`). It then makes sure to run the given command only for those projects as well as all projects depending on them since they might be affected by the change too.

This helps save computation by reducing the set of projects that need to be processed.

### Local Computation Caching

Nx comes with a so-called [computation caching](https://nx.dev/using-nx/caching) feature. For every cacheable operation, Nx takes a set of input parameters, computes a hash and stores the result.

![Command, flags, source files, environment variables and more getting passed to a hasher which computes a number used to cache the task result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0454eh0kh52b0e8da6bn.png)

Whenever a hash matches, the computation is not run, but rather the previous result is restored. This can dramatically speed up things and avoid running any computation that has already been run previously.

### Distributed Remote Caching (with Nx Cloud)

By default, the Nx computation cache is stored locally (usually within the `node_modules/.cache/nx` folder). The real benefits come from sharing it with others, that being your co-workers or CI agents.

[Nx Cloud](https://nx.app) allows to distribute the Nx computation cache across machines.

![How the local cache gets replicated to other machines via Nx Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv9w7f0k0yrxs7c277j9.png)

Connecting an existing Nx workspace to Nx Cloud can be done with

```
nx connect-to-nx-cloud
```

[More on the docs](https://nx.dev/nx-cloud/set-up/set-up-caching). Nx Cloud comes with [500 hours of computation time saved per month](https://nx.app/pricing) which is plenty for most workspaces. If you go over, you can buy more, or in the worst case, caching simply stops until the next month.

## Bonus! Lerna can do this too!!

[Nrwl](https://nrwl.io), the company behind [Nx](https://nx.dev), recently [took over stewardship of Lerna](https://dev.to/nrwl/lerna-is-dead-long-live-lerna-3jal). Meanwhile, Lerna 5.4 just got released which features a nice integration with Nx, allowing existing Lerna users to keep using the very same commands, but still benefit from the improved task scheduling and caching abilities Nx comes with.

How to enable it? [Read more on the Lerna docs](https://lerna.js.org/docs/features/cache-tasks)

---

## Learn more

- 🧠 [Nx Docs](https://nx.dev)
- 👩‍💻 [Nx GitHub](https://github.com/nrwl/nx)
- 💬 [Nrwl Community Slack](https://go.nrwl.io/join-slack)
- 📹 [Nrwl Youtube Channel](https://www.youtube.com/nrwl_io)
- 🥚 [Free Egghead course](https://egghead.io/courses/scale-react-development-with-nx-4038)
- 🧐 [Need help with Angular, React or Nx? Talk to us 😃](https://nrwl.io/contact-us)

Also, if you liked this, click the :heart: and make sure to follow [Juri](https://twitter.com/juristr) and [Nx](https://twitter.com/nxdevtools) on Twitter for more!

{% embed https://dev.to/t/nx %}