Live Note

Remain optimistic

React Fast Refresh 是 React 官方推荐的 HMR 解决方案,旨在提供快速、可靠的热更新体验,同时保留组件的本地状态(例如 useStateuseRef 的值)。它由 Facebook 团队开发,广泛集成到现代构建工具(如 Vite 和 Next.js)中。

背景与目标

传统 HMR 的问题

  • 在传统的 HMR 实现中,当模块更新时,整个组件树可能会被重新渲染,导致本地状态丢失。
  • 对于 React 组件,状态丢失会影响开发体验,例如表单输入值或动画状态被重置。

React Fast Refresh 的目标

  • 提供快速的模块更新。
  • 保留 React 组件的本地状态。
  • 只重新渲染受影响的组件,而不是整个应用。

核心原理

React Fast Refresh 通过在编译时注入特定的运行时代码,并在运行时与 React 协调器(React Reconciler)协作,实现高效的热更新。

Read more »

Vite 为什么快?

Vite 的速度优势主要来源于其基于原生 ESM(ES Modules)的开发模式和高效的构建过程

原生 ESM 的开发服务器

传统打包工具(如 Webpack)的问题

  • Webpack 在开发模式下会将所有模块打包成一个或多个 bundle 文件,即使只修改了一个文件,也需要重新打包整个 bundle。
  • 这种打包过程涉及大量的文件读取、解析、转换和写入操作,随着项目规模增加,启动时间和热更新时间会显著变慢。

Vite 的解决方案

  • Vite 利用现代浏览器对 原生 ES Modules(ESM) 的支持,在开发模式下不进行打包,而是直接通过 HTTP 请求按需加载模块。
  • 浏览器支持 <script type="module">,可以动态加载 ESM 模块,Vite 的开发服务器将源码直接以 ESM 格式提供给浏览器。
  • 启动速度快:
    • Vite 的开发服务器启动时,只需要初始化一个简单的 HTTP 服务器,无需预先打包整个项目。
    • 文件按需加载,只有访问到的模块才会被处理(例如解析和转换),大大减少了初始启动时间。
  • 按需编译:
    • Vite 使用 Esbuild(一个极快的 JavaScript/TS 编译器)对 TypeScript、JSX 等非标准 ESM 文件进行预编译,生成浏览器可识别的 ESM 格式。
    • Esbuild 的性能比传统工具(如 Babel)快 10-100 倍,因为它是基于 Go 语言实现的,充分利用了多核 CPU。
Read more »

Delete Service Worker on Safari

To delete a service worker on Safari, you can follow these steps:

  1. Open Safari Developer Console (Press Command + Option + C)
  2. Copy and paste the following JavaScript code into the console:
1
2
3
4
5
6
7
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.unregister().then(function () {
console.log("Service Worker Unregistered:", registration.scope)
})
}
})

This code will:

  1. Get all registered service workers using navigator.serviceWorker.getRegistrations()
  2. Loop through each registration
  3. Unregister each service worker using the unregister() method
  4. Log a confirmation message with the scope of the unregistered service worker

You can verify that the service worker has been successfully removed by:

  1. Opening Safari Developer Tools
  2. Going to the Application tab
  3. Checking the Service Workers section - it should now be empty

Note: After unregistering the service worker, you might need to close all tabs and restart Safari for the changes to take full effect.

Introduction

I’ve recorded an MP4 demo to showcase the results. In the video, you’ll see the Markdown content being displayed
character by character, with dynamic rendering and automatic scrolling.

Demo MP4

In this article, I’ll walk you through a simple yet powerful demo of implementing stream output in JavaScript. This
demo
mimics the behavior of ChatGPT-like applications, where responses are displayed in a streaming fashion, and the
content
is dynamically rendered as Markdown. The best part? It’s incredibly simple to implement, and I’ll provide the full
source code and an MP4 demo to showcase the results.

Read more »

In modern web development, enhancing user experience by highlighting specific text within a larger body of content is a common requirement. Whether it’s for search results, annotations, or emphasizing key information, a Highlight Text component can be incredibly useful. In this article, we’ll walk through the process of building a simple yet effective Highlight Text component using React.

Here is the Example captured:

Highlight Text Component Example

Read more »

In this post, I want to share how I built a custom slider component that comes packed with useful features. This slider not only supports basic functionalities like step increments and configurable maximum/minimum values, but it also offers advanced features such as displaying scale tick values, suffix rendering, and custom value formatting. Whether you’re building a finance dashboard, a settings panel, or a data visualization tool, this component can be tailored to your needs.

Features

Step Increments

The slider supports step increments, allowing you to specify how much the slider’s value should increase or decrease with each move. This is particularly useful for ensuring users select values within a desired precision.

Scale Tick Values

To improve usability, the slider renders tick values along its track. These ticks provide a visual reference for users, making it easier to gauge the range and current value.

Maximum and Minimum Values

You can easily define the slider’s range by setting the maximum and minimum values. This ensures that users can only select a value within a valid, predetermined range.

Suffix Rendering

Often, slider values need to be accompanied by a unit (e.g., %, $, kg). With suffix rendering, you can easily append a suffix to the displayed value, making it clear what the number represents.

Custom Value Formatting

In some cases, the raw slider value might not be in the ideal format for display. This slider component allows you to pass a formatter function that converts the numeric value into a user-friendly string format.

Implementation Overview

Below is a simplified example of how the slider component is implemented using React and TypeScript:

Read more »

事业略有起色

3 月份从工厂离职了,真正意义上的工厂.每天除了当客服,收发快递,还有搬货/送货/扫地/修电脑/修网线… 如果真的说学到了什么东西, 那就是: “穷死不耕丈人田,饿死不打亲戚工”. 用最少的钱去让你干最多的活, 美其名曰”教学”, 实际上资源也不给, 就等你一个人摸爬滚打. 算了, 各自安好.

在厂里做过的工作

  • 收发快递
  • 收货/送货
  • 赶生产
  • 客服
  • 店铺运营
  • TikTok 运营
  • 摄影/美工/后期
  • 司机
  • 跑腿
  • 修电脑/修网线

工资: 5k-6k/月, 工作时间: 9:00-19:30, 工作日为 6 天/周.

4 月离职了, 找了一家做数字货币相关的公司,负责 React Native. 在我这小地方还是不错的, 因为工作岗位没那么多, 就将就下吧. 公司里有个老大哥, 看起来 blog 里面摸索过非常多的东西, 但是实际的代码真是一言难尽, 也可能是老项目的原因吧, 太多遗留了, 初期规划不好+没有彻底的理解, 就会导致项目越来越混乱.
项目最开始应该是要走 MVC 模式的, 但是不知道为什么写着写着就变成了 MVVM 模式, 导致代码混乱, 后期维护起来也很麻烦. 很多地方还会存在 State 刷新滞后问题, 因为整体是没有一个状态管理的, 靠的是强制更新最底层的一个 state 去整体刷新 page. 非常不友好.

奶奶送回老家了,说起来也有意思,四十来岁就跟着二伯父了, 劳累到七十多, 有点脑萎缩(记忆力减退,智商减退),二伯父一脚踢给大伯父了,哈哈. 所以说, 心不黑,不能成商啊. 准备过年回去看一下, 毕竟这种事小辈也不好掺和. 大伯父都不说话, 我还说什么呢? 不能交真心.

没什么想说的了, 年终总结也就是记流水账, 希望自己能坚持下去, 2025 继续努力.

去年的目标

Opps… 才发现去年没有写总结呢, 那就用前年的目标吧.

  • 英语,需要到精通需要学习上万小时,努力吧 : 正在努力学习
  • 攒钱了,先把婚姻大事搞定 : 嗯… 开销有点大, 今年存… flag
  • 目前的几本书先看完 : 说来也算看完了吧, 纸质书+微信读书. 而且我觉得微信读书非常好
  • 陪女朋友出省旅游一次 : 已经去了 南昌/南京/杭州 今年继续努力.
  • 日语还是慢慢来吧 : 在用 Duolingo 学习,但是学到后面还是得上书本. Duolingo 只是非常基础非常基础的东西,让你开口, 仅此而已. 真要系统学习还是得上书本.
  • 电池健康管理 : 无了, 已从电池厂离职

今年的目标

嗯…又多了一些爱好

  1. 英语: 老生常谈了, 不赘述
  2. 日语: 努力学习, 以后可以看看日本的机会
  3. 工资: 努力提高到 3 开头吧, 大概率是实现不了
  4. 创业: 准备看看有没有什么项目了, 拿点钱来做点事情, 预计会上独立站/阿里国际站, 还在计划中.
  5. 结婚: 运气好的话, 应该可以搞定吧
  6. 旅游: 或许能上一次高原呢?

Node.js is built on Google’s V8 engine, and V8’s garbage collection mechanism is one of its core performance features. In this article, we’ll explore the three primary garbage collection algorithms used in V8: Mark-Sweep, Mark-Compact, and Scavenge, along with their working principles and application scenarios.

V8’s Memory Partition Model

Before diving into garbage collection algorithms, we need to understand V8’s memory partition model. V8 divides memory into two main regions:

Young Generation

  • Contains objects with short lifespans.
  • Typically small in size (e.g., a few MB).
  • Uses the Scavenge algorithm for garbage collection.

Old Generation

  • Contains objects with longer lifespans.
  • Typically larger in size (e.g., hundreds of MB).
  • Uses the Mark-Sweep and Mark-Compact algorithms for garbage collection.
Read more »

Boosting Flutter Performance with Frame-Based Rendering using keframe

Flutter is known for its smooth UI performance, but complex animations or frequent rebuilds can sometimes lead to jank. Frame-based rendering, also known as keyframe animation, offers a powerful technique to optimize these scenarios. This article explores how to leverage frame-based rendering in Flutter using the keframe package, focusing on FrameAnimation, FrameSeparateWidget, and SizeCacheWidget.

What is Frame-Based Rendering?

Traditional rendering in Flutter often involves rebuilding widgets every time their properties change. While efficient in many cases, this can become a bottleneck when dealing with intricate animations or rapidly changing data.

Frame-based rendering pre-calculates animation frames and stores them. During animation, the system displays these pre-rendered frames sequentially, minimizing constant rebuilds. This results in smoother animations and improved performance, especially on less powerful devices.

Read more »