Skip to content

Commit 828dd1f

Browse files
committed
add package manager notes
1 parent a52992a commit 828dd1f

File tree

11 files changed

+144
-0
lines changed

11 files changed

+144
-0
lines changed

session 1/chapters/references.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
- https://blog.bitsrc.io/javascript-internals-javascript-engine-run-time-environment-settimeout-web-api-eeed263b1617
1212

1313
- https://hackernoon.com/understanding-js-the-event-loop-959beae3ac40
14+
15+
- [event-loop](https://nodejs.dev/learn/the-nodejs-event-loop)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Nodejs
2+
3+
Node.js is an open-source, cross-platform, JavaScript runtime environment.
4+
5+
Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.
6+
7+
A Node.js app runs in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms
8+
9+
When Node.js performs an I/O operation, like reading from the network, accessing a database or the filesystem, instead of blocking the thread and wasting CPU cycles waiting, Node.js will resume the operations when the response comes back.
10+
11+
This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency
12+
13+
# How NodeJs handles thousands of concurrent connections with a single server
14+
15+
![jsconcurrent](../images/HowJsHandlesConcurrency.jpg)
16+
17+
[source](https://stackoverflow.com/questions/34855352/how-in-general-does-node-js-handle-10-000-concurrent-requests)
18+
19+
Think of NodeJS as a waiter taking the customer orders while the I/O chefs prepare them in the kitchen. Other systems have multiple chefs, who take a customers order, prepare the meal, clear the table and only then attend to the next customer.
20+
21+
In a conventional multi-threaded web server, you will typically have a thread for each request being handled, and it will process ONLY that request until it's finished. What happens if you have a lot of slow requests? You end up with a lot of your threads hanging around processing these requests, and other requests (which might be very simple requests that could be handled very quickly) get queued behind them.
22+
23+
![multithread-vs-single](../images/multithread-vs-single.jpg)
24+
25+
### Magic, invisible threading
26+
27+
The seemingly mysterious thing is how both the approaches above manage to run workload in "parallel"? The answer is that the database is threaded. So our single-threaded app is actually leveraging the multi-threaded behaviour of another process: the database.
28+
29+
### Where singlethreaded approach fails
30+
31+
A singlethreaded app fails big if you need to do lots of CPU calculations before returning the data. Now, I don't mean a for loop processing the database result. That's still mostly O(n). What I mean is things like doing Fourier transform (mp3 encoding for example), ray tracing (3D rendering) etc.
32+
33+
Another pitfall of singlethreaded apps is that it will only utilise a single CPU core. So if you have a quad-core server (not uncommon nowdays) you're not using the other 3 cores.
34+
35+
### Where multithreaded approach fails
36+
37+
A multithreaded app fails big if you need to allocate lots of RAM per thread. First, the RAM usage itself means you can't handle as many requests as a singlethreaded app. Worse, malloc is slow. Allocating lots and lots of objects (which is common for modern web frameworks) means we can potentially end up being slower than singlethreaded apps. This is where node.js usually win.
38+
39+
One use-case that end up making multithreaded worse is when you need to run another scripting language in your thread. First you usually need to malloc the entire runtime for that language, then you need to malloc the variables used by your script.
40+
41+
So if you're writing network apps in C or go or java then the overhead of threading will usually not be too bad. If you're writing a C web server to serve PHP or Ruby then it's very easy to write a faster server in javascript or Ruby or Python.
42+
43+
### Hybrid approach
44+
45+
Some web servers use a hybrid approach. Nginx and Apache2 for example implement their network processing code as a thread pool of event loops. Each thread runs an event loop simultaneously processing requests single-threaded but requests are load-balanced among multiple threads.
46+
47+
Some single-threaded architectures also use a hybrid approach. Instead of launching multiple threads from a single process you can launch multiple applications - for example, 4 node.js servers on a quad-core machine. Then you use a load balancer to spread the workload amongst the processes.
48+
49+
In effect the two approaches are technically identical mirror-images of each other.
50+
51+
### Some more details on the architecture
52+
53+
Given a NodeJS application, since Node is single threaded, say if processing involves a Promise.all that takes 8 seconds, does this mean that the client request that comes after this request would need to wait for eight seconds? No.
54+
55+
**NodeJS event loop is single threaded. The entire server architecture for NodeJS is not single threaded.**
56+
57+
// TODO add more details on this
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Software Package Manager
2+
3+
A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner. A package manager deals with packages, distributions of software and data in archive files.
4+
5+
A package manager is a programming language’s tool to create project environments and easily import external dependencies. You don’t have to reinvent the wheel and are able to make the most of the tools at your disposal. When working on a project or library, you may “package” your project and publish it for others.
6+
7+
You can usually specify dependencies, a package name, author, tags/keywords and version number.
8+
9+
# What is NPM?
10+
11+
NPM stands for the Node Package Manager.
12+
13+
npm is the package manager for JavaScript. It is the world’s largest software repository. npm hosts extremely popular packages like jQuery, Bootstrap, React, Angular etc. Linking your GitHub repository with npm also allows you to create and share your own projects. As the npm online repository is so large and diverse, JavaScript front-end and Node.js backend developers make use of npm as the packages can be used in either environment.
14+
15+
NPM is bundled with Node.js Runtime. It is Node.js Default Package Manager. In other words, when you install Node.js, NPM gets installed.
16+
17+
![npm-basic-commands](../images/npm-basic-commands.jpg)
18+
19+
Every NPM package will be installed in the `node_module` folder. Running `npm install <module>` installs the latest package version available in the NPM registry.
20+
21+
**Installing a Specific Package Version**
22+
23+
```
24+
npm install lodash@4.17.19 ## install lodash version 4.17.19
25+
```
26+
27+
**Uninstalling a Local Package**
28+
29+
```
30+
npm uninstall <package-name>
31+
```
32+
33+
![npm-dependencies](../images/npm-dependencies.jpg)
34+
35+
**DevDependencies**
36+
37+
DevDependencies are the packages that are not required by the app to run. These packages are used for development purposes such as testing, local server speeding for development purposes, transpiring code, etc.
38+
39+
```
40+
npm install nodemon --save-dev ## install a dev package to our dependencies
41+
```
42+
43+
![dev-dep](../images/npm-dev-dependencies.jpg)
44+
45+
## Package.json Dependencies Management
46+
47+
The main aim of using package.json is to define your application’s dependencies.
48+
49+
## Package-lock.json
50+
51+
In version 5, npm introduced the package-lock.json file.
52+
53+
The goal of package-lock.json file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.
54+
55+
This file “locks down” your dependency versions. That way whenever someone else runs yarn install or npm install, they’ll receive the exact dependencies versions listed out in the lock file. This ensures that your team has the identical package versions as you do. It also helps prevent bugs that can appear due to the introduction of updated, untested package versions.
56+
57+
![package-lock](../images/package-lock.jpg)
58+
59+
When a package is installed, it is saved with a caret (^) before the version number in the dependencies scaffold. The caret tells NPM always install the highest version available for this package that matches the major version available in the project’s dependencies.
60+
61+
But if the package-lock.json file is available in that project, NPM will match the version specified in the lock file.
62+
63+
![why lock file](../images/package-lock.jpg)
64+
65+
# What is Yarn?
66+
67+
Yarn is a JavaScript package manager created by Facebook. Yarn stands for Yet Another Resource Negotiator. It provides similar functionalities as NPM. It is an alternative to NPM when installing, uninstalling, and managing package dependencies from the NPM registry or GitHub repositories.
68+
69+
# NPM vs Yarn
70+
71+
One of the main difference between NPM and Yarn is how they handle the package installation process. Yarn installs packages in parallel. Yarn is optimized to fetch and install multiple packages simultaneously.
72+
73+
When you install a package, these two package managers save offline cache. You can then install a package you installed before from the memory cache even when you are offline. Yarn has a well-managed offline cache.
74+
75+
# NPM vs Yarn new updates
76+
77+
Yarn and NPM are continually updating to improve on their current features, as well as adding new features such as NPX and PnP.
78+
79+
**NPX**
80+
81+
NPX stands for Node Package Executor. It is a new addition to NPM version 5.2.0 or higher. NPX helps you to execute one-off commands. With NPX, you can execute packages from the NPM registry without installing them to your project dependencies.
82+
83+
There are more features that you can benefit from using NPX. Check this guide to learn more about NPX.
84+
85+
for more information, [read here](https://www.section.io/engineering-education/npm-vs-yarn-which-one-to-choose/)

session 3/chapters/resources.md

Whitespace-only changes.
206 KB
Loading
164 KB
Loading
121 KB
Loading
31.5 KB
Loading
25.4 KB
Loading

session 3/images/package-lock.jpg

138 KB
Loading

0 commit comments

Comments
 (0)