Skip to content

Commit 75a6e4a

Browse files
committed
add: refactoring.md
1 parent 336b25d commit 75a6e4a

5 files changed

+60
-5
lines changed

_posts/2022-10-16-Font&Performance.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Fonts and Performance
33
date: 2022-10-16 22:24:45 +0800
4-
categories: [front-end]
4+
categories: [front-end, performance]
55
tags: [fonts, optimization]
66
---
77
When building modern websites, fonts play a crucial role in how your content is perceived. However, including custom fonts, especially external fonts, can negatively impact performance. This guide will help you decide whether to include external fonts and how to do so with the least impact on your site's performance.

_posts/2022-10-17-MobilePerformance.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: Mobile Performance
33
date: 2022-10-17 16:33:50 +0800
4-
categories: [front-end]
5-
tags: [mobile, optimization, performance]
4+
categories: [front-end, performance]
5+
tags: [mobile, optimization]
66
---
77

88
With web access on mobile devices becoming increasingly prevalent, ensuring optimal performance is more critical than ever. Mobile platforms now offer full-featured web browsers, but they often come with constraints like limited bandwidth, reduced CPU power, and limited battery life. This guide explores key strategies and best practices for improving mobile-specific performance, ensuring that your web content is fast, efficient, and user-friendly.

_posts/2022-10-25-WebpackNote.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Webpack and Optimization Introduction
33
date: 2022-10-25 01:33:19 +0800
4-
categories: [front-end]
4+
categories: [front-end, tools]
55
tags: [webpack] # TAG names should always be lowercase
66
---
77

_posts/2022-09-23-readinglist.md renamed to _posts/2023-09-23-readinglist.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: September Reading List Recommended
3-
date: 2022-09-23 08:52:39 +0800
3+
date: 2023-09-23 08:52:39 +0800
44
categories: [ReadingList]
55
tags: [videos, articles]
66
---

_posts/2024-11-26.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: From Clunky to Clean: Refactoring a JavaScript Image Toggle
3+
date: 2023-11-26 01:30:11 +0800
4+
categories: [front-end,js]
5+
tag: [refactoring,js]
6+
---
7+
8+
A simple image toggle can teach us key JS optimization principles. Let's break down the refactoring process.
9+
10+
## Starting Point
11+
Here's a basic implementation most developers might write:
12+
13+
```javascript
14+
function picLink(event) {
15+
event.preventDefault();
16+
let allImages = document.querySelectorAll("img");
17+
let selectedImg = document.querySelector(`img[id="${this.dataset.img}"]`);
18+
19+
if (selectedImg.style.display === "block") {
20+
selectedImg.style.display = "none";
21+
} else {
22+
allImages.forEach(img => {
23+
img.style.display = img.id === selectedImg.id ? "block" : "none";
24+
});
25+
}
26+
}
27+
```
28+
29+
## The Problems
30+
- Repeated DOM queries hammer performance
31+
- `this` binding is fragile
32+
- Complex selector logic
33+
- Nested conditionals hurt readability
34+
35+
## Clean Solution
36+
```javascript
37+
const images = document.querySelectorAll("img");
38+
39+
const toggleImage = ({currentTarget}) => {
40+
const targetImage = document.getElementById(currentTarget.dataset.img);
41+
images.forEach(img =>
42+
img.style.display = img === targetImage ?
43+
(img.style.display === "block" ? "none" : "block") :
44+
"none"
45+
);
46+
};
47+
```
48+
49+
Key changes:
50+
- Cache DOM selections
51+
- Destructure event object
52+
- Direct ID lookup
53+
- Single expression toggle
54+
55+
This cleaner version runs faster and reads better. More importantly, it reveals core JS patterns that improve any codebase.

0 commit comments

Comments
 (0)