From 402387b01b519950cee0ecb5a11e9886f7f08ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gromit=20=28=EC=A0=84=EB=AF=BC=EC=9E=AC=29?= <64779472+ssi02014@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:41:54 +0900 Subject: [PATCH] perf(omitBy): improve omitBy performance (#671) * fix: improve omitBy performance * fix: fix omitBy ts-doc * fix: for in to Object.keys * Update src/object/omitBy.ts --------- Co-authored-by: Sojin Park --- benchmarks/performance/omitBy.bench.ts | 7 +++---- src/compat/object/omit.ts | 2 +- src/object/omitBy.ts | 10 ++++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/benchmarks/performance/omitBy.bench.ts b/benchmarks/performance/omitBy.bench.ts index dd439d40..00964900 100644 --- a/benchmarks/performance/omitBy.bench.ts +++ b/benchmarks/performance/omitBy.bench.ts @@ -6,15 +6,14 @@ const omitByToolkit = omitByToolkit_; const omitByLodash = omitByLodash_; describe('omitBy', () => { + const obj = { a: 1, b: 'omit', c: 3, d: 'test', e: 0 }; + const shouldOmit = (value: number | string) => typeof value === 'string'; + bench('es-toolkit/omitBy', () => { - const obj = { a: 1, b: 'omit', c: 3 }; - const shouldOmit = (value: number | string) => typeof value === 'string'; omitByToolkit(obj, shouldOmit); }); bench('lodash/omitBy', () => { - const obj = { a: 1, b: 'omit', c: 3 }; - const shouldOmit = (value: number | string) => typeof value === 'string'; omitByLodash(obj, shouldOmit); }); }); diff --git a/src/compat/object/omit.ts b/src/compat/object/omit.ts index 0dffbe71..ff16859f 100644 --- a/src/compat/object/omit.ts +++ b/src/compat/object/omit.ts @@ -30,7 +30,7 @@ export function omit, K extends keyof T>(obj: T, k * * @template T - The type of object. * @param {T} obj - The object to omit keys from. - * @param {...(PropertyKey | PropertyKey[] | PropertyKey[][]} keys - A variable number of keys to be omitted from the object. + * @param {...(PropertyKey | PropertyKey[] | PropertyKey[][])} keys - A variable number of keys to be omitted from the object. * @returns {Partial} A new object with the specified keys omitted. */ export function omit< diff --git a/src/object/omitBy.ts b/src/object/omitBy.ts index 997c971b..2e8cf989 100644 --- a/src/object/omitBy.ts +++ b/src/object/omitBy.ts @@ -13,7 +13,7 @@ * * @example * const obj = { a: 1, b: 'omit', c: 3 }; - * const shouldOmit = (key, value) => typeof value === 'string'; + * const shouldOmit = (value) => typeof value === 'string'; * const result = omitBy(obj, shouldOmit); * // result will be { a: 1, c: 3 } */ @@ -23,9 +23,11 @@ export function omitBy>( ): Partial { const result: Partial = {}; - const objEntries = Object.entries(obj); - for (let i = 0; i < objEntries.length; i++) { - const [key, value] = objEntries[i]; + const keys = Object.keys(obj); + + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + const value = obj[key]; if (!shouldOmit(value, key)) { (result as any)[key] = value; }