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 <raon0211@gmail.com>
This commit is contained in:
Gromit (전민재) 2024-10-06 17:41:54 +09:00 committed by GitHub
parent 55cae8b0eb
commit 402387b01b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 9 deletions

View File

@ -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);
});
});

View File

@ -30,7 +30,7 @@ export function omit<T extends Record<string, any>, 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<T>} A new object with the specified keys omitted.
*/
export function omit<

View File

@ -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<T extends Record<string, any>>(
): Partial<T> {
const result: Partial<T> = {};
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;
}