What magic is this? Faster npm with PnPm

Node.js is the most popular javascript runtime environments and it is extremely popular amongst developers. It uses npm as its package manager. Another alternative to npm is yarn which is touted to be more performant (i.e. faster than npm), however not fast enough.

Issues With Npm/Yarn

The main issue that many developers have with npm is that it is slow when used on projects with really big dependecies, also it does not manage the creation of the node_modules efficiently, duplicating dependencies across multiple projects.

Introducing PnPm

Here comes PnPm, a highly performant (fast) new package manager for node.js with the efficient disk space management. Pnpm is a drop in replacement for npm, it incorporates all the features of npm and it does it more efficiently.

Features of PnPm

  • Up to 2 times faster than npm
  • Shareable node_modules content, i.e – Files inside node_modules are stored in a single content-addressable storage, this is then cloned or hard-linked to projects where the libraries are used. This can greatly reduce disk space usage by removing the need for duplications.
  • PnPm Workspaces – This allows developers to isolate concerns by using multiple packages in a repository.
  • Peers installation
  • Patching dependencies and so much more…

Enough Talk, How do you install it?

To install PnPm is very easy, explore the following options

  1. Install using npm

To install PnPm using npm, there are two available packages for the pnpm CLI, namely, pnpm and @pnpm/exe.

  • pnpm is a ordinary version of pnpm, which needs Node.js to run.
  • @pnpm/exe is the version packaged with Node.js into an executable, so it can be executed on a system with no Node.js installed.
$ npm install -g pnpm

or 

$ npm install -g @pnpm/exe

 2. On macOs, using Homebrew

If you are on macOs, you can use the brew package manager to install pnpm as follows.

$ brew install pnpm

You can see other installation methods over here

How to use PnPm

That is the interesting thing, it is a drop-in replacement for npm, so all npm commands apply to pnpm also, just replace npm with pnpm, e.g

  • To install all dependencies, run pnpm install
  • To install a package, run pnpm install @faker-js/faker
  • etc.

Introduction to Logical Shift And Its Use Cases

If like me, you have a degree in computer science, you must have encountered logical shifts during the course of your academic studies, if not, if you have learnt pretty much any programming language, you would have encountered them. The issue is that most people treat them as something to be aware of but not really understood. If you give me a couple of minutes, I hope to help simplify it to you.

What is logical shift

Logical shift is a type of bitwise operation that shifts/moves the bits in its operands in a particular direction. In simple terms, if you logically shift X by Y, it means that the operation will move the bits in X by Y positions. Still sounds geeky, let us break it down together.

There are two types of logical shifts, namely

  1. Right Shift (denoted by >> )
  2. Left Shift (denoted by << )

If you consider the definition above, Right shift means that the bits in (X) would be shifted to the right by Y places, also Left shift means that the bits in (X) would be shifted to the left by Y places.

Example of Left Shift

let X = 4 and Y = 2, lets find X << Y

Convert X  to binary, then you have

X = 100  (i.e. the binary representation of 4 is 0000 0100 )

Note: you should use the 8 digits representation

If you find the binary calculation confusing, please read up about it here.

Now, to calculate (X << Y), i.e we move the bits in X  to the left by 2 places.

  1. before move X = 0000 0100
  2. after left move (1) , X = 0000 1000
  3. after leftmove (2) , X = 0001 0000

Now we convert (0001 0000) back to decimal and we have 2.

Hence, 4 << 2  == 16.

Example of Right Shift

let X = 8 and Y = 2, lets find X >> Y

Convert X  to binary, then you have

X = 1000  (i.e. the binary representation of 8 is 0000 1000).

Now, to calculate (X >> Y), i.e we move the bits in X to the right by 2 places.

  1. before move X = 0000 1000
  2. after right move (1) , X = 0000 0100
  3. after right move (2) , X = 0000 0010

Note: since the first 4 digits in the binary value of X is (0) and we are shifting to the right, we could have discared it and just used (1000).

Now we convert (0010) back to decimal and we have 2.

Hence, 8  >> 2  is equal to 2.

Use cases of logical shifts (both left and right).

Left shift (<<) is used to multiply a value by any power of 2 and Right bit shift (>>) is used to divide by any power of two. consider the table below

Cheatsheet or Ways of calulating the logical shifts faster

The left logical shift (<<) is the same as

Left Bit Shift
# Operation X Y Resolution Result
1 X << Y 8 2 8 * pow(2,2) 32
2 X << Y 2 4 2 * pow(2,4) 32
3 X << Y 3 2 3 * pow(2,2) 12

and,

Right Bit Shift
# Operation X Y Resolution Result
1 X >> Y 64 4 64 / pow(2,4) 4
2 X >> Y 4 2 4 / pow(2,2) 1
3 X >> Y 8 2 8 / pow(2,2) 2

So, that is all. Hope i have made the use of logical shift alot clearer to you.

giphy1