Skip to content

Translation: Profiler #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/content/reference/react/Profiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: <Profiler>

<Intro>

`<Profiler>` lets you measure rendering performance of a React tree programmatically.
`<Profiler>` cho phép bạn đo lường hiệu suất render của một React tree bằng cách lập trình

```js
<Profiler id="App" onRender={onRender}>
Expand All @@ -18,11 +18,11 @@ title: <Profiler>

---

## Reference {/*reference*/}
## Tham khảo {/*reference*/}

### `<Profiler>` {/*profiler*/}

Wrap a component tree in a `<Profiler>` to measure its rendering performance.
Hãy bọc các component bên trong `<Profiler>` để đo lường hiệu suất.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be "Hãy đặt" instead of "Hãy bọc"


```js
<Profiler id="App" onRender={onRender}>
Expand All @@ -32,41 +32,41 @@ Wrap a component tree in a `<Profiler>` to measure its rendering performance.

#### Props {/*props*/}

* `id`: A string identifying the part of the UI you are measuring.
* `onRender`: An [`onRender` callback](#onrender-callback) that React calls it every time components within the profiled tree update. It receives information about what was rendered and how much time it took.
* `id`: Một chuỗi ký tự để xác định phần giao diện mà bạn muốn đo lường hiệu suất.
* `onRender`: Một [callback `onRender`](#onrender-callback) React sẽ gọi mỗi khi các component bên trong tree cập nhật. Nó sẽ nhận lại thông tin về những gì đã được render và mất bao nhiêu thời gian để render chúng.

#### Caveats {/*caveats*/}
#### Những điều cần chú ý {/*caveats*/}

* Profiling adds some additional overhead, so **it is disabled in the production build by default.** To opt into production profiling, you need to enable a [special production build with profiling enabled.](https://fb.me/react-profiling)
* Việc đo lường sẽ làm giảm hiệu suất, vì vậy **mặc định nó sẽ luôn được vô hiệu hoá trong bản build production.** Để đo lường ở production, bạn cần kích hoạt [tính năng đo lường trong một bản build production đặc biệt.](https://fb.me/react-profiling)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "trong phiên bản production" will be better than "trong bản build production"


---

### `onRender` callback {/*onrender-callback*/}

React will call your `onRender` callback with information about what was rendered.
React sẽ gọi callback `onRender` với thông tin về những gì được render.

```js
function onRender(id, phase, actualDuration, baseDuration, startTime, commitTime) {
// Aggregate or log render timings...
}
```

#### Parameters {/*onrender-parameters*/}
#### Các tham số {/*onrender-parameters*/}

* `id`: The string `id` prop of the `<Profiler>` tree that has just committed. This lets you identify which part of the tree was committed if you are using multiple profilers.
* `phase`: `"mount"`, `"update"` or `"nested-update"`. This lets you know whether the tree has just been mounted for the first time or re-rendered due to a change in props, state, or hooks.
* `actualDuration`: The number of milliseconds spent rendering the `<Profiler>` and its descendants for the current update. This indicates how well the subtree makes use of memoization (e.g. [`memo`](/reference/react/memo) and [`useMemo`](/reference/react/useMemo)). Ideally this value should decrease significantly after the initial mount as many of the descendants will only need to re-render if their specific props change.
* `baseDuration`: The number of milliseconds estimating how much time it would take to re-render the entire `<Profiler>` subtree without any optimizations. It is calculated by summing up the most recent render durations of each component in the tree. This value estimates a worst-case cost of rendering (e.g. the initial mount or a tree with no memoization). Compare `actualDuration` against it to see if memoization is working.
* `startTime`: A numeric timestamp for when React began rendering the current update.
* `endTime`: A numeric timestamp for when React committed the current update. This value is shared between all profilers in a commit, enabling them to be grouped if desirable.
* `id`: Chuỗi `id` prop của `<Profiler>` tree được thực thi (commit). Điều này cho phép bạn xác định phần nào của tree đã được thực thi nếu bạn đang sử dụng nhiều trình đo hiệu năng.
* `phase`: `"mount"`, `"update"` hoặc `"nested-update"`. Chúng giúp bạn biết liệu tree đã được render lần đầu tiên hay đã được render lại do thay đổi trong props, state hoặc hooks.
* `actualDuration`: Số mili giây dành cho việc render `<Profiler>` và các phần tử con phục vụ cho cập nhật hiện tại. Nó giúp cho thấy subtree tận dụng tốt việc ghi nhớ (memorization) (ví dụ như [`memo`](/reference/react/memo) [`useMemo`](/reference/react/useMemo)). Lý tưởng nhất là giá trị này sẽ giảm đáng kể sau lần render ban đầu vì nhiều component con chỉ cần được render lại nếu props cụ thể của chúng thay đổi.
* `baseDuration`: Số mili giây ước tính thời gian cần để render lại toàn bộ subtree của `<Profiler>` mà không cần tối ưu. Giá trị này được tính bằng cách tính tổng thời gian render gần đây nhất của các component trong tree. Nó sẽ giúp ước tính được thời gian chậm nhất (ví dụ lần render ban đầu hay một tree mà không có sự ghi nhớ nào). Hãy so sánh `actualDuration` với nó để xem việc ghi nhớ có hoạt động chính xác hay không.
* `startTime`: Mốc thời gian mà React bắt đầu thực hiện việc render cho cập nhật hiện tại.
* `endTime`: Mốc thời gian React thực hiện xong cập nhật hiện tại. Giá trị này được chia sẻ giữa tất cả các profiler trong một lần thực thi, cho phép nhóm chúng lại nếu cần thiết.

---

## Usage {/*usage*/}
## Cách sử dụng {/*usage*/}

### Measuring rendering performance programmatically {/*measuring-rendering-performance-programmatically*/}
### Đo hiệu suất render bằng cách lập trình {/*measuring-rendering-performance-programmatically*/}

Wrap the `<Profiler>` component around a React tree to measure its rendering performance.
Bọc `<Profiler>` vào một React tree để đo lường hiệu suất render.

```js {2,4}
<App>
Expand All @@ -77,25 +77,25 @@ Wrap the `<Profiler>` component around a React tree to measure its rendering per
</App>
```

It requires two props: an `id` (string) and an `onRender` callback (function) which React calls any time a component within the tree "commits" an update.
Nó yêu cầu 2 props: một chuỗi `id` và một callback `onRender` mà React gọi bất cứ khi nào một component trong tree cập nhật.

<Pitfall>

Profiling adds some additional overhead, so **it is disabled in the production build by default.** To opt into production profiling, you need to enable a [special production build with profiling enabled.](https://fb.me/react-profiling)
Việc đo lường sẽ làm giảm hiệu suất, vì vậy **mặc định nó sẽ luôn được vô hiệu hoá trong bản build production.** Để đo lường ở production, bạn cần kích hoạt [tính năng đo lường trong một bản build production đặc biệt.](https://fb.me/react-profiling)

</Pitfall>

<Note>

`<Profiler>` lets you gather measurements programmatically. If you're looking for an interactive profiler, try the Profiler tab in [React Developer Tools](/learn/react-developer-tools). It exposes similar functionality as a browser extension.
`<Profiler>` giúp bạn đo lường hiệu suất bằng cách lập trình thủ công. Nếu bạn muốn tìm một phương pháp đo lường có thể tương tác tốt hơn, hãy thử tab Profiler ở trong [công cụ phát triển React](/learn/react-developer-tools). Nó là một tiện ích trình duyệt với các tính năng tương tự.

</Note>

---

### Measuring different parts of the application {/*measuring-different-parts-of-the-application*/}
### Đo hiệu suất các phần khác nhau của ứng dụng {/*measuring-different-parts-of-the-application*/}

You can use multiple `<Profiler>` components to measure different parts of your application:
Bạn có thể sử dụng nhiều `<Profiler>` để đo lường nhiều phần khác nhau của ứng dụng:

```js {5,7}
<App>
Expand All @@ -108,7 +108,7 @@ You can use multiple `<Profiler>` components to measure different parts of your
</App>
```

You can also nest `<Profiler>` components:
Bạn cũng có thể lồng nhiều `<Profiler>` vào với nhau:

```js {5,7,9,12}
<App>
Expand All @@ -126,7 +126,6 @@ You can also nest `<Profiler>` components:
</App>
```

Although `<Profiler>` is a lightweight component, it should be used only when necessary. Each use adds some CPU and memory overhead to an application.
Mặc dù `<Profiler>` là một component rất nhẹ, nó chỉ nên được dùng khi cần thiết. Mỗi lần sử dụng sẽ đều tiêu hao CPU và tốn thêm bộ nhớ cho ứng dụng.

---