1. Introduction
The echo service I work on has a VoC (Voice of Customer) board. Customers post requests or issues they are experiencing, and developers respond with comments on the detail page and handle them by changing their status. One day, a customer reported an issue with the comment feature on the VoC detail page.
Entering comments is really slow right now. I can type quickly elsewhere.
I opened the same screen right after receiving the report, and I was able to reproduce the issue. When I typed in the comment box, the cursor would freeze, then the characters would appear all at once. During this process, Korean character composition would sometimes break, resulting in incorrect input. But when I returned to the same page that afternoon, the lag was gone, and I could no longer reproduce what I had seen earlier. I might have dismissed this bug as “Unable to reproduce,” but fortunately, the video the customer attached and what I had witnessed firsthand helped me identify and fix the issue.
2. Suspected Cause
2-1. Controlled Input and the Scope of Re-rendering
The most common way to handle input fields in React is with a controlled component. The input value is stored in state and passed down as valueand updated in onChange. The VoC detail page in question used this approach, too.
const VocPostDetailDeveloperView = ({ detail, onAddComment, ... }) => {
const [commentText, setCommentText] = useState(''); // 댓글 입력값이 화면 최상위 state
const handleCommentChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCommentText(event.target.value); // 한 글자마다 화면 전체 리렌더
};
return (
// ... 본문, 첨부, 스냅, 댓글 목록, 상태 버튼 ...
<TextField multiline value={commentText} onChange={handleCommentChange} />
);
};
The code looks textbook-perfect at first glance. The problem, however, was that commentTextwas at the top level of the detail page component's state. When state changes, React re-renders the component and its children. Every keystroke was re-rendering the entire detail page—not just the comment box, but also the content, attachment list, snaps, comment list, and status buttons.
Normally, this alone would not cause a noticeable slowdown. Virtual DOM comparisons are fast, and only one part of the actual DOM—the input field—changes. The issue was that one of the functions called during rendering by those re-rendered components was more expensive than expected.
2-2. The Multilingual Label Function label()
The Vizend frontend uses @vizendjs/accent’s label()from the shared library to translate UI text. label('Comment')looks up and returns the translation from the label dictionary for the current language. The label dictionary is fetched from the server and cached in the browser's localStorage. Since button names, status names, and guidance text all go through this function, label()is called dozens of times, and sometimes more than a hundred times, during a single render of the detail page.
3. Measuring the Situation
At first, I suspected that the issue might be caused by the page becoming sluggish when a post had many attachments. To recreate an environment similar to the one in the customer's video, I attached two videos to a post in my local environment and tried entering a comment. However, there was no meaningful difference compared with a post without attachments. No network requests or reloading of attachments occurred while typing, either. The attachments were not the cause.
So I read through the detail page code again. As noted earlier, the input value was stored in state at the top level of the page. What caught my eye were the label()calls wrapped around text throughout the page. I counted 42 in the Customer detail component and 54 in the Developer detail component. Since the entire page was re-rendered with every keystroke, all of these calls were being run again on every keystroke, too. That would not be a problem if the function were lightweight, but when I opened the accent bundle code, I found that label()retrieved the label dictionary from localStorage and called JSON.parseevery time it was invoked.
So I measured how many times label()was actually called per keystroke and how much it cost. In the browser console, I temporarily wrapped localStorage.getItemand JSON.parseto count calls and measure their duration, then typed characters into the input one at a time to calculate the average for a single keystroke. The results below are for the lightest post, with no attachments, snaps, or comments.
|
Metric |
Per keystroke |
|---|---|
|
label() calls |
142 |
|
JSON.parse calls |
1,278 |
|
Size of parsed string |
About 15 MB |
|
Share of total processing time spent in JSON.parse |
55% |
Even on the shortest post, the label dictionary was being parsed 1,278 times for every character entered, and more than half of the processing time was spent in JSON.parse.
4. Root cause
4-1. A single call to label() was expensive
I traced through the internals of label() in accent 7.2.8, which echo-front used, and found no cache. To make the bundled code easier to read, each call followed this path:
// accent 7.2.8 — label() 이 호출될 때마다 거치는 경로 (읽기 쉽게 정리)
static getEpisodeLabelMap(context, language) {
const lang = this.getLanguage(); // localStorage 'vizend.language' 읽기 + 파싱
const cache = this.getLabelCache(); // localStorage 'vizend.lingo.labels' 읽기 + JSON.parse
const index = cache.episodes.findIndex(/* pavilion · episode · 언어 일치 */);
this.removeLegacyLabel(context); // localStorage.removeItem 4회
return { ...cache.episodes[index].labels }; // 라벨 맵을 통째로 복사
}
Because the entire label dictionary was parsed and copied every time, the cost of a single call to label() was proportional to the size of the dictionary. I kept enlarging only the label dictionary in the browser's localStorage and measured the same screen again. The number of label() calls stayed the same, but the cost per call increased significantly with the dictionary size.
|
Label dictionary size |
Number of labels |
One keystroke (before the change) |
|---|---|---|
|
11KB |
262 |
33.1ms |
|
262KB |
3,930 |
262.3ms |
|
1,105KB |
15,982 |
1,457.7ms |
|
2,969KB |
42,182 |
4,488.5ms |
4-2. The entire screen rerendered with every character
The second cause, as mentioned earlier, was that the input value was stored in top-level state, so the entire screen was redrawn with every keystroke. Each time, label() was called once for every piece of text on the screen.
To see how much each of the two causes contributed, I temporarily replaced JSON.parse in the browser console so that it would parse the same string only once, then ran the same typing test again. This simulated what would happen if accent had a cache, without changing the code. As a result, the time per keystroke dropped from about 63ms to 18ms, a 72% reduction. The remaining 18ms was the cost of redrawing the entire screen. In other words, the delay was growing as the cost of a single call to label() was multiplied by the increased number of calls caused by rerendering. In summary, the cost per keystroke was roughly determined as follows.
키 입력 1회 비용 ≈ label() 1회 비용(라벨 사전 크기) × 다시 그려지는 문구 수(화면 요소 수)
누적 지연 ≈ 키 입력 1회 비용 × 글 길이
This meant that the perceived performance could vary significantly depending on the account, language, and post. Even in environments with a small dictionary, long posts could still cause delays.
5. Solution
5-1. First attempt: Move the input value into a child component
I initially split the comment composition area into a separate component (VocCommentCompose) and moved the input value state into it. Since only the small composition area rerendered when a key was pressed, the number of label() calls per keystroke on the VoC Developer detail screen dropped from 156 to 4, and the time decreased from 22.7ms to 3.6ms. However, when I measured the VoR detail screen after enlarging the label dictionary, it took 17.3ms even with the same change—exceeding the 16ms target (one frame at 60fps). Even just the four remaining calls to label() inside the composition area could exceed the target when the dictionary was large.
5-2. Second attempt: Switch to an uncontrolled input
So I changed the input value to be held in a ref instead of state. With an uncontrolled component, the DOM holds the value, and React reads it only when needed. Since typing no longer changes state, no rerender occurs, and the number of label() calls per keystroke becomes zero.
// 코드 예시
export const VocCommentCompose = ({ avatarInitial, onSubmit }: VocCommentComposeProps) => {
const inputRef = useRef<HTMLTextAreaElement>(null); // 입력값은 state 가 아니라 ref
const handleSubmit = async () => {
const message = (inputRef.current?.value ?? '').trim();
if (!message || !onSubmit) return;
await onSubmit(message);
if (inputRef.current) inputRef.current.value = ''; // 성공했을 때만 DOM 을 직접 비운다
};
return (
<>
<TextField multiline defaultValue="" inputRef={inputRef}
placeholder={label('Add a comment...')} />
<Button onClick={handleSubmit}>{label('Comment')}</Button>
</>
);
};
The parent screen only passes what to register (onSubmit) and knows nothing about the input values. React.memowas another way to prevent the parent from rerendering, but it would require setting up memoization boundaries for each child, which meant many places to modify and made the solution fragile, so I didn't choose it. Comparing the three approaches under the same load conditions gives the following results.
|
Approach |
VoC Developer details |
Effect of dictionary size |
|---|---|---|
|
Original code (top-level state) |
162.42ms |
Proportional |
|
1st: child state |
8.19ms |
Reduced, but still present |
|
2nd: uncontrolled (ref) |
0.47ms |
None |
5-3. Improving the shared accent library
The most fundamental fix would be to add caching to label()However, accent is a shared library used by all of Vizend's frontend apps, so any impact would extend to every app. So I planned to first fix the rerendering issue in echo-front, where the changes would be contained, and then compile the measurement results for the accent team. But on checking, I found that the latest version (7.2.10) already had logic to memoize the parsed result using the raw localStorage string as the key. echo-front was pinned to 7.2.8, so upgrading to the latest version became the next step.
6. Behaviors preserved when switching to uncontrolled inputs
When switching to uncontrolled inputs, React no longer knows the values, so behaviors previously handled with state can quietly disappear. To improve performance without changing the behavior, I also made sure to preserve the following:
-
Disable the submit button when the field is empty: Instead of tracking the input value, I used a boolean state to track only whether the field was empty, updating it only when that value changed. Rerenders occur only when the first character is entered or the last character is deleted.
-
Clear the input field: Only when registration succeeded, I cleared it directly with inputRef.current.value = '', leaving the entered text as is if the request failed.
-
Prevent duplicate submissions: In React 18 StrictMode, side effects inside a state updater run twice, so I moved the registration request outside the updater and confirmed that only one comment was created.
-
Input field in the form: I moved the Label field on the registration screen to a react-hook-form field without splitting it into a separate component. react-hook-form is uncontrolled by default, so the title and body fields on the same screen were already fast. MUI's TextFieldreceives the ref for the actual input element via inputRef, so I passed them separately as shown below.
const { ref: labelInputRef, ...labelField } = register('label');
<TextField {...labelField} inputRef={labelInputRef} />
7. Conclusion
The biggest lesson from this work is that where you store input values determines the scope of rerendering. The input was a conventional controlled input, but its state lived at the top level of the screen, and when combined with a function that incurred a cost on every render, a single keystroke took tens to hundreds of milliseconds. Frequent events like typing should be confined to the editing area or handled with uncontrolled inputs; for forms, it's best to let react-hook-form handle them. Also, instead of giving up when the issue couldn't be reproduced, measuring what the cost was proportional to first helped me find the cause.
Wrio