Web Optimization Concepts - The one and only guide you need (In progress)
Published: 2021-12-06 19:52:57

"How to fully optimize web performance? You'll understand after reading this article"</br>
</br>
</br>
</br>
</br>
</br>
## Table Of Contents 
[1. What is and how to use CDN](#c1)

- [1.1 What is CDN exactly?](#c12)

- [1.2 What's the benefits of CDN](#c13)

[2. Why do we need Lazy Load](#c2)
[3. Reflow and Repaint](#c3)

</br>
</br>
</br>
## Other Contents
[HTML - The one and only guide you need (in progress)](https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga)
[React Concepts Part.1 - The one and only guide you need](https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki)
[React Concepts Part.2 - The one and only guide you need](https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib) </br>
[CSS Concepts - The one and only guide you need](https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6) 
[Computer Network Concepts - The one and only guide you need](https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k)
[Browser Concepts - The one and only guide you need](https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni) </br>
</br>
</br>
</br>

### 1. What is CDN and how to apply CDN <a name="c1"></a>

**1.1 What is CDN exactly?** <a name="c11"></a>

CDN (Content Delivery Network) refers to a computer network system connected to each other through the closest server. So that content such as music, pictures, videos, applications and other static resources can be send to users more reliable and efficiently. CDN provides high-performance, scalability and low-cost network content to users.

**1.2 What's the benefits of CDN** <a name="c12"></a>

**In terms of performance, the role of introducing CDN is to:**

- Users receive content from the nearest server, with lower latency , faster content loading speed.

- Part of the resource request is allocated to the CDN, reducing the load on the server

**In terms of network security, CDN helps defend against DDoS, MITM and other network attacks.**</br>
</br>
</br>
</br>
### 2. Why do we need Lazy Load <a name="c2"></a>

Lazy loading is also called on-demand loading. It refers to the lazy loading of image data in long page website. 

In a relatively long web page, if many pictures all loaded at the same time but the user can only see the part of the picture in the viewport, it wastes a lot of performance.

The above problems can be solved. Before scrolling the screen, pictures outside the visualization area will not be loaded, it will only be loaded when it shows up in the viewport. This makes the page load faster and reduces the load on the server.

**How do we achieve lazy load**

In HTML5, `image` tag has an attribute call `data-src` to temporarily store resource path as we would not want it to be load immediately. After that we will keep monitoring the relative postion between the image and viewport. Once it appears in the viewport, we will switch the data in `data-src` to `src`. 

**There are two ways to monitoring the relative postion between the image and viewport.**

**_1. Calculate the height manually_**
 
- `window.innerHeight`
The read-only innerHeight property of the Window interface returns the interior height of the window in pixels.

- `document.body.scrollTop`
The Element.scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically.

- `imgs.offsetTop`
The HTMLElement.offsetTop read-only property returns the distance of the outer border of the current element relative to the inner border of the top of the offsetParent node.

if `img.offsetTop < window.innerHeight + document.body.scrollTop`, it indicates that the image has entered viewport

```javascript
<div class="container">
     <img src="loading.gif"  data-src="pic.png">
     <img src="loading.gif"  data-src="pic.png">
     <img src="loading.gif"  data-src="pic.png">
</div>
<script>
var imgs = document.querySelectorAll('img');
function lazyLoad(){
		var scrollTop = document.body.scrollTop;
		var winHeight= window.innerHeight;
		for(var i=0;i < imgs.length;i++){
			if(imgs[i].offsetTop < scrollTop + winHeight ){
				imgs[i].src = imgs[i].getAttribute('data-src');
			}
		}
	}
  window.onscroll = lazyLoad();
</script>
```

**_2. Intersection Observer API_**

[Read more about implementation about Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)

The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.

The Intersection Observer API allows you to configure a callback that is called when either of these circumstances occur:

A target element intersects either the device's viewport or a specified element. That specified element is called the root element or root for the purposes of the Intersection Observer API.
The first time the observer is initially asked to watch a target element.

### 3. Reflow and Repaint <a name="c3"></a>

[read more about it here](https://medium.com/swlh/what-the-heck-is-repaint-and-reflow-in-the-browser-b2d0fb980c08) 

**- Repaint**
As the name suggests repaint is nothing but the repainting element on the screen as the skin of element change which affects the visibility of an element but do not affects layout.

Example.

1. Changing visibility of an element.

2. Changing an outline of the element.

3. Changing background.

Would trigger a repaint.
According to Opera, the repaint is an expensive operation as it forces the browser to verify/check the visibility of all other dom nodes.

**- Reflow**
Reflow means re-calculating the positions and geometries of elements in the document, for the purpose of re-rendering part or all of the document. Because reflow is a user-blocking operation in the browser, it is useful for developers to understand how to improve reflow time and also to understand the effects of various document properties (DOM depth, CSS rule efficiency, different types of style changes) on reflow time. Sometimes reflowing a single element in the document may require reflowing its parent elements and also any elements which follow it.

Repaints and reflows can be expensive, they can hurt the user experience, and make the UI appear sluggish

**How to reduce reflow and repaint?**

- When manipulating the DOM, try to operate on lower-level DOM nodes

- Use absolute or fixed to keep elements out of the document flow so that their changes will not affect other elements

- To avoid frequent manipulation of the DOM, you can create a `documentFragment`, apply all DOM operations on it, and finally update it to the document. Why not use React?

**How to optimize Animation?**

Same as above mentioned. Use absolute or fixed to keep elements out of the document flow so that their changes will not affect other elements.