Skip to content

refactor: 역방향 객체 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 - #391

Open
ssi02014 wants to merge 3 commits into
toss:mainfrom
ssi02014:perf/getJongseong
Open

ssi02014 wants to merge 3 commits into
toss:mainfrom
ssi02014:perf/getJongseong

Conversation

@ssi02014

@ssi02014 ssi02014 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Overview

역방향 조회용 객체를 만드는 로직이 각 함수에 흩어져 있고, 일부는 호출·순회마다 역방향 객체를 새로 생성하는 비효율인 부분이 있었습니다.
공통 invert 유틸을 추가해 역방향 객체 생성을 일원화하고, combineVowels와 getJongseong이 이를 사용하도록 리팩터링했습니다.

문제 상황 (as-is)

1. combineVowels — 호출마다 O(n) 선형 탐색

export function combineVowels<V1 extends string, V2 extends string>(vowel1: V1, vowel2: V2) {
  return (Object.entries(DISASSEMBLED_VOWELS_BY_VOWEL).find(([, value]) => value === `${vowel1}${vowel2}`)?.[0] ??
    `${vowel1}${vowel2}`) as ...;
}
  • 함수를 호출할 때마다 Object.entries(...).find(...)로 역방향 탐색을 선형(O(n))으로 수행합니다.

2. getJongseong — 역방향 객체를 매 순회마다 재생성

const JONGSEONGS_COMPOSITE = JONGSEONGS.slice(1).map(
  d => Object.fromEntries(
    Object.entries(DISASSEMBLED_CONSONANTS_BY_CONSONANT).map(([key, val]) => [val, key])
  )[d]
);
  • Object.fromEntries(...)로 만드는 역방향 객체가 .map 콜백이 실행될 때마다 매번 새로 생성됩니다.
  • JONGSEONGS.slice(1)의 길이만큼 동일한 객체를 반복 생성합니다. → 사실상 O(n²)

변경 사항 (to-be)

1. 공통 invert 유틸 추가 (src/_internal)

공통 invert 유틸을 추가해 역방향 객체 생성을 일원화하고, combineVowels와 getJongseong이 이를 사용하도록 리팩터링했습니다.

export function invert<K extends PropertyKey, V extends PropertyKey>(obj: Record<K, V>): Record<V, K> {
  const result = {} as Record<V, K>;
  const keys = Object.keys(obj) as K[];

  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    const value = obj[key];
    result[value] = key;
  }

  return result;
}

2. combineVowels 적용

호출마다 entries를 생성하고,find로 탐색하던 로직을, 모듈 스코프에서 invert로 1회 생성한 역방향 객체를 통해 조회(O(1))하도록 변경했습니다.

const VOWEL_BY_DISASSEMBLED_VOWELS = invert(DISASSEMBLED_VOWELS_BY_VOWEL);

export function combineVowels<V1 extends string, V2 extends string>(vowel1: V1, vowel2: V2) {
  return (VOWEL_BY_DISASSEMBLED_VOWELS[`${vowel1}${vowel2}` as keyof CombineVowel] ??
    `${vowel1}${vowel2}`) as `${V1}${V2}` extends keyof CombineVowel ? CombineVowel[`${V1}${V2}`] : `${V1}${V2}`;
}

3. getJongseong 적용

동일하게 역방향 객체를 모듈 스코프에서 한 번만 생성하도록 분리합니다.

const CONSONANT_BY_DISASSEMBLED_CONSONANT = invert(DISASSEMBLED_CONSONANTS_BY_CONSONANT);

const JONGSEONGS_COMPOSITE = JONGSEONGS.slice(1).map(d => CONSONANT_BY_DISASSEMBLED_CONSONANT[d]);

개선 포인트

  • 역방향 객체 생성 로직을 invert 유틸로 일원화 (중복 제거, 재사용성 확보)
  • combineVowels: 호출마다 entries 생성 후 find 탐색 → 모듈 스코프에서 역방향 객체 생성 후 O(1) 객체 조회로 개선
  • getJongseong: JONGSEONGS.slice(1)`의 길이만큼(약 27회) 동일한 객체를 반복 생성하는 것을 사전 생성 후 조회로 개선

영향도

  • combineVowels, getJongseong의 동작은 동일하며, 기존 테스트는 모두 통과합니다.
  • 내부 구현만 개선한 리팩터링입니다.

PR Checklist

  • I read and included theses actions below
  1. I have read the Contributing Guide
  2. I have written documents and tests, if needed.

@vercel

vercel Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
es-hangul Error Error Sep 22, 2026 2:44am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 780d9d6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ssi02014 ssi02014 changed the title refactor(getJongseong): 종성 역방향 매핑 객체를 매 순회마다 생성하던 로직 개선 refactor(getJongseong/combineVowels): 역맵 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 Jul 19, 2026
@ssi02014 ssi02014 changed the title refactor(getJongseong/combineVowels): 역맵 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 refactor: 역맵 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 Jul 19, 2026
@ssi02014 ssi02014 changed the title refactor: 역맵 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 refactor: 역방향 객체 생성 로직을 공통 invert 유틸로 분리하고 combineVowels·getJongseong에 적용 Jul 19, 2026

This branch had an error being deployed

1 failed deployment
Preview — 780d9d64 Deployed Sep 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant