mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
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:
parent
55cae8b0eb
commit
402387b01b
@ -6,15 +6,14 @@ const omitByToolkit = omitByToolkit_;
|
||||
const omitByLodash = omitByLodash_;
|
||||
|
||||
describe('omitBy', () => {
|
||||
bench('es-toolkit/omitBy', () => {
|
||||
const obj = { a: 1, b: 'omit', c: 3 };
|
||||
const obj = { a: 1, b: 'omit', c: 3, d: 'test', e: 0 };
|
||||
const shouldOmit = (value: number | string) => typeof value === 'string';
|
||||
|
||||
bench('es-toolkit/omitBy', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
@ -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<
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user