DEV Community

Cover image for Helping the Environment by Saving Two Centuries of Compute time
Juri Strumpflohner for Nx

Posted on • Originally published at blog.nrwl.io

Helping the Environment by Saving Two Centuries of Compute time

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

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 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)

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

See all the details

Last 30-day savings correspond to:

CO2 emission savings for the last 30 days thanks to Nx Cloud

See all the details

Since beginning of Nx Cloud:

CO2 emission savings since the beginning of Nx Cloud

See all the details

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


Enter fullscreen mode Exit fullscreen mode

Nx affected commands 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 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

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 allows to distribute the Nx computation cache across machines.

How the local cache gets replicated to other machines via Nx Cloud

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



nx connect-to-nx-cloud


Enter fullscreen mode Exit fullscreen mode

More on the docs. Nx Cloud comes with 500 hours of computation time saved per month 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, the company behind Nx, recently took over stewardship of Lerna. 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


Learn more

Also, if you liked this, click the ❤️ and make sure to follow Juri and Nx on Twitter for more!

#nx

Top comments (0)