Skip to content

Commit 17bd771

Browse files
Merge branch 'CodeHarborHub:main' into main
2 parents 38e607b + 4cda6bb commit 17bd771

File tree

116 files changed

+8469
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+8469
-408
lines changed

blog/introduction-to-web-assembly.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: 'Introduction to WebAssembly: Enhancing Web Performance'
3+
sidebar_label: WebAssembly and Web Performance
4+
authors: [nayanika-mukherjee]
5+
tags: [webassembly, wasm, web performance, technology]
6+
date: 2024-07-21
7+
hide_table_of_contents: true
8+
---
9+
10+
## Introduction
11+
12+
WebAssembly (Wasm) is a binary instruction format that provides near-native performance for web applications. Designed as a portable compilation target for high-level languages like C, C++, and Rust, WebAssembly enables efficient execution of code on modern web browsers. This documentation introduces WebAssembly, its benefits, and how to get started with Wasm development.
13+
14+
## What is WebAssembly (Wasm)?
15+
16+
WebAssembly is a low-level, assembly-like language with a compact binary format that runs with near-native performance. It provides a new way to run code written in multiple languages on the web at near-native speed, allowing for powerful web applications.
17+
18+
## Why Use WebAssembly?
19+
20+
WebAssembly offers several advantages:
21+
22+
- **Performance:** Wasm code executes at near-native speeds, making it ideal for performance-critical applications like games, simulations, and complex calculations.
23+
- **Portability:** Code compiled to Wasm can run on any modern web browser, providing a consistent execution environment across different platforms.
24+
- **Interoperability:** Wasm integrates seamlessly with JavaScript, enabling the use of existing web technologies and frameworks.
25+
- **Security:** Wasm operates in a safe, sandboxed execution environment, reducing the risk of security vulnerabilities.
26+
27+
## How WebAssembly Works
28+
29+
WebAssembly works by compiling high-level code into a binary format that can be executed by the browser's virtual machine. The process involves several steps:
30+
31+
1. **Source Code:** Write your code in a high-level language like C, C++, or Rust.
32+
2. **Compilation:** Use a compiler to convert the source code into WebAssembly binary format (`.wasm` file).
33+
3. **Execution:** The browser's virtual machine executes the Wasm binary, providing near-native performance.
34+
35+
## Setting Up Your Environment
36+
37+
To start developing with WebAssembly, you'll need to set up your development environment. This includes installing the necessary tools and compilers.
38+
39+
### Setting Up a Development Environment
40+
41+
1. **Install Node.js:** Node.js is required for various Wasm development tools.
42+
2. **Install a Compiler:** Depending on your source language, install a suitable compiler. For C/C++, install Emscripten. For Rust, install the Rust toolchain.
43+
44+
### Compiling to WebAssembly
45+
46+
To compile your code to WebAssembly, follow these steps:
47+
48+
1. **Write Your Code:** Write your application in C, C++, Rust, or another supported language.
49+
2. **Compile:** Use your compiler to generate the Wasm binary. For example, with Emscripten, use the following command:
50+
```bash
51+
emcc your_code.c -o your_code.wasm
52+
```
53+
For Rust:
54+
```
55+
rustc --target wasm32-unknown-unknown -O your_code.rs
56+
```
57+
## Interfacing with JavaScript
58+
59+
WebAssembly can interact with JavaScript, enabling you to call Wasm functions from JavaScript and vice versa. Use the JavaScript WebAssembly API to load and instantiate Wasm modules.
60+
61+
## Debugging WebAssembly Code
62+
63+
Debugging Wasm code involves using browser developer tools and other utilities:
64+
65+
- Browser DevTools: Modern browsers provide debugging support for WebAssembly, including breakpoints, step execution, and variable inspection.
66+
- Source Maps: Generate source maps to map Wasm code back to the original source code for easier debugging.
67+
- Optimizing WebAssembly Performance
68+
69+
To optimize Wasm performance:
70+
71+
- Optimize Code: Write efficient, performance-oriented code in the source language.
72+
- Compiler Flags: Use compiler optimization flags to improve the performance of the generated Wasm binary.
73+
- Profiling: Use profiling tools to identify and address performance bottlenecks.
74+
75+
## Case Studies and Real-World Examples
76+
77+
Explore case studies and real-world examples of WebAssembly in action:
78+
79+
- Gaming: High-performance games running in the browser.
80+
- Data Visualization: Complex data visualizations with real-time interactivity.
81+
- Scientific Simulations: Web-based simulations for scientific research and education.
82+
83+
## Conclusion
84+
85+
WebAssembly is a powerful technology that enhances web performance and expands the capabilities of web applications. By leveraging Wasm, developers can build high-performance, portable, and secure applications that run seamlessly across different browsers and platforms. This documentation provides a comprehensive guide to getting started with WebAssembly, covering essential concepts, tools, and best practices.

courses/Next.Js/Intermediate-level/Data-Fetching/Handling.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ Handling errors in data fetching is crucial to provide a good user experience.
6363
</div>
6464
);
6565
}
66-
```
66+
```
67+
68+
**Output:**
69+
<BrowserWindow >
70+
<div>
71+
Failed to load: 404 server not found
72+
</div>
73+
</BrowserWindow>

courses/Next.Js/Intermediate-level/Data-Fetching/fetching-Data.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,18 @@ export default function Home() {
102102
</div>
103103
);
104104
}
105-
```
105+
```
106+
107+
**Output:**
108+
109+
<BrowserWindow url="http://localhost:3000/post/3">
110+
<h1>Posts</h1>
111+
<ul>
112+
<li>Post 1</li>
113+
<li>Post 2</li>
114+
<li>Post 3</li>
115+
<li>Post 4</li>
116+
<li>Post 5</li>
117+
<li>Post 6</li>
118+
</ul>
119+
</BrowserWindow>

courses/Next.Js/advance-level/Advance-Routing/Customizing.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,57 @@ Route-level code splitting ensures that only the necessary code for the current
8080
2. **Using `dynamic` for Code Splitting with Custom Loading**:
8181
```javascript
8282
// pages/index.js
83+
import { useState } from 'react';
8384
import dynamic from 'next/dynamic';
8485
8586
const DynamicComponentWithCustomLoading = dynamic(() => import('../components/DynamicComponent'), {
8687
loading: () => <p>Loading...</p>,
8788
});
8889
8990
export default function Home() {
91+
const [loadDynamicComponent, setLoadDynamicComponent] = useState(false);
92+
93+
const handleClick = () => {
94+
setLoadDynamicComponent(true);
95+
};
9096
return (
9197
<div>
9298
<h1>Home Page</h1>
93-
<DynamicComponentWithCustomLoading />
99+
<button onClick={handleClick}>Load Dynamic Component</button>
100+
{loadDynamicComponent && <DynamicComponentWithCustomLoading />}
94101
</div>
95102
);
96103
}
97104
```
98-
105+
**Output:**
106+
<BrowserWindow>
107+
<div>
108+
<h1 style={{fontSize:"1.5rem"}}>Home Page</h1>
109+
<button onClick={()=>{
110+
let button=document.getElementById("button")
111+
button.style.display="none"
112+
let Interval=setInterval(()=>{
113+
let Dynamic=document.getElementById("Dynamic")
114+
let datas=["1st Content","2nd Content","3rd Content"]
115+
let Index
116+
if(Dynamic && Dynamic.textContent && Dynamic.textContent.length>1){
117+
if(Dynamic.textContent==="Switch Dynamic Content"){
118+
Index=0
119+
}
120+
else{
121+
if(datas.indexOf(Dynamic.textContent)==2){
122+
Index=0
123+
}
124+
else{Index=datas.indexOf(Dynamic.textContent)+1}
125+
}
126+
Dynamic.textContent=datas[Index]
127+
}
128+
else{
129+
clearInterval(Interval)
130+
return
131+
}
132+
},1000)
133+
}} id="button" style={{padding:"0.8rem 2rem",fontWeight:"600",borderRadius:"0.6rem",border:"0.1rem solid",margin:"0.5rem"}}>Load Dynamic Component</button>
134+
<h2 style={{fontSize:"1.3rem"}} id="Dynamic">Switch Dynamic Content</h2>
135+
</div>
136+
</BrowserWindow>

courses/Next.Js/advance-level/Advance-data-fetching/dynamic-generation.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ You can combine static and dynamic data fetching to build more flexible and perf
3131
export default function Home({ staticData }) {
3232
const [dynamicData, setDynamicData] = useState(null);
3333
34-
useEffect(() => {
34+
function handleClick(){
3535
// Fetch dynamic data on client-side
3636
fetch('https://api.example.com/dynamic-data')
3737
.then((res) => res.json())
3838
.then((data) => setDynamicData(data));
39-
}, []);
39+
}
4040
4141
return (
4242
<div>
4343
<h1>Combining Static and Dynamic Data Fetching</h1>
4444
<h2>Static Data</h2>
4545
<pre>{JSON.stringify(staticData, null, 2)}</pre>
4646
<h2>Dynamic Data</h2>
47+
<button onClick={handleClick}>click to Fetch</button>
4748
{dynamicData ? (
4849
<pre>{JSON.stringify(dynamicData, null, 2)}</pre>
4950
) : (
@@ -53,7 +54,44 @@ You can combine static and dynamic data fetching to build more flexible and perf
5354
);
5455
}
5556
```
56-
57+
**Output:**
58+
<BrowserWindow url="http://localhost:3000/users">
59+
<div>
60+
<h1 style={{fontSize:"1.5rem"}}>Combining Static and Dynamic Data Fetching</h1>
61+
<h2 style={{fontSize:"1.3rem"}}>Static Data</h2>
62+
<pre style={{margin:"2rem auto"}}>{`${JSON.stringify([{ id:"1",
63+
name:"siva",
64+
info:"Developer"
65+
},{
66+
id:"2",
67+
name:"Kumar",
68+
info:"Developer"
69+
}],null,2)}`}</pre>
70+
<h2 style={{fontSize:"1.3rem"}}>Dynamic Data</h2>
71+
<button onClick={()=>{
72+
let text=document.getElementById("text")
73+
let loading=document.getElementById("loading")
74+
let details=[{
75+
id:"11",
76+
name:"Rohan",
77+
info:"Jr.Developer"
78+
},{
79+
id:"15",
80+
name:"Mano",
81+
info:"Sr.Developer"
82+
}]
83+
loading.textContent="Loading..."
84+
setTimeout(() => {
85+
text.style.display="block"
86+
loading.textContent=" "
87+
text.textContent=JSON.stringify(details, null, 2)
88+
}, 2000)
89+
}} style={{padding:"1rem 2rem",border:"none",borderRadius:"0.6rem",color:"white",background:"black",fontWeight:"600",cursor:"pointer"}}>click to Fetch</button>
90+
<br />
91+
<p id="loading" style={{margin:"2rem auto"}}></p>
92+
<pre id="text" style={{margin:"2rem auto",display:"none"}}></pre>
93+
</div>
94+
</BrowserWindow>
5795
#### Optimizing Data Fetching for Performance
5896

5997
1. **Minimizing API Calls**:
@@ -86,6 +124,21 @@ You can combine static and dynamic data fetching to build more flexible and perf
86124
}
87125
```
88126

127+
**Output:**
128+
<BrowserWindow url="http://localhost:3000/data">
129+
<div>
130+
<h1 style={{fontSize:"1.5rem"}}>Optimized Data Fetching</h1>
131+
<pre style={{margin:"2rem auto"}}>{`${JSON.stringify([{ id:"1",
132+
name:"siva",
133+
info:"Developer"
134+
},{
135+
id:"2",
136+
name:"Kumar",
137+
info:"Developer"
138+
}],null,2)}`}</pre>
139+
</div>
140+
</BrowserWindow>
141+
89142
3. **Prefetching Data**:
90143
- Prefetch data for links using the `next/link` component to improve navigation speed.
91144
```javascript
@@ -103,6 +156,22 @@ You can combine static and dynamic data fetching to build more flexible and perf
103156
}
104157
```
105158

159+
**Output:**
160+
<BrowserWindow>
161+
<div>
162+
<h1 style={{fontSize:"1.5rem"}} id="Text">Home Page</h1>
163+
<button style={{textDecoration:"underline",color:"blue",border:"none",background:"transparent",cursor:"pointer"}}
164+
onClick={()=>{
165+
let text=document.getElementById("Text")
166+
text.textContent="About page"
167+
let button=document.getElementById("button")
168+
button.style.display="none"
169+
}}
170+
id="button">Go to About</button>
171+
</div>
172+
</BrowserWindow>
173+
174+
106175
4. **Using SWR for Client-side Caching**:
107176
- `SWR` (Stale-While-Revalidate) is a React Hooks library for data fetching that provides caching and revalidation.
108177
```javascript
@@ -119,4 +188,4 @@ You can combine static and dynamic data fetching to build more flexible and perf
119188
return <div>Data: {JSON.stringify(data)}</div>;
120189
}
121190
```
122-
191+

courses/Next.Js/advance-level/Advance-data-fetching/static-generation.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Static generation with revalidation allows you to generate static pages at build
4141
}
4242
```
4343

44+
45+
4446
#### Server-side Rendering with Caching Strategies
4547

4648
Server-side rendering (SSR) can be optimized using caching strategies to improve performance and reduce server load.
@@ -73,4 +75,26 @@ Server-side rendering (SSR) can be optimized using caching strategies to improve
7375

7476
2. **Using Cache-Control Headers**:
7577
- `s-maxage=10`: Cache the response on the server for 10 seconds.
76-
- `stale-while-revalidate`: Serve stale content while revalidating in the background.
78+
- `stale-while-revalidate`: Serve stale content while revalidating in the background.
79+
80+
**Output:**
81+
<BrowserWindow url="http://localhost:3000/users">
82+
<div>
83+
<h1>Static Generation with Revalidation</h1>
84+
<button onClick={()=>{
85+
let text=document.getElementById("text")
86+
let details=[{
87+
id:"1",
88+
name:"siva",
89+
info:"Developer"
90+
},{
91+
id:"2",
92+
name:"John",
93+
info:"Developer"
94+
}]
95+
text.textContent=JSON.stringify(details, null, 2)
96+
}} style={{padding:"1rem 2rem",border:"none",borderRadius:"0.6rem",color:"white",background:"black",fontWeight:"600",cursor:"pointer"}}>click to Fetch</button>
97+
<br />
98+
<pre id="text" style={{margin:"2rem auto"}}></pre>
99+
</div>
100+
</BrowserWindow>

0 commit comments

Comments
 (0)