diff --git a/.eslintrc.js b/.eslintrc.js index cb1219533278..fbc686f003b4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -177,6 +177,7 @@ module.exports = { // ESLint core rules 'es/no-nullish-coalescing-operators': 'off', 'es/no-optional-chaining': 'off', + 'prefer-object-spread': 'off', // Import specific rules 'import/consistent-type-specifier-style': ['error', 'prefer-top-level'], diff --git a/contributingGuides/STYLE.md b/contributingGuides/STYLE.md index 6af3a82c2ff6..3c40de6dc4ae 100644 --- a/contributingGuides/STYLE.md +++ b/contributingGuides/STYLE.md @@ -33,6 +33,7 @@ - [`var`, `const` and `let`](#var-const-and-let) - [Object / Array Methods](#object--array-methods) - [Accessing Object Properties and Default Values](#accessing-object-properties-and-default-values) +- [Object Merging: Spread Operator vs. Object.assign](#object-merging-spread-operator-vs-objectassign) - [JSDocs](#jsdocs) - [Component props](#component-props) - [Destructuring](#destructuring) @@ -737,6 +738,27 @@ const name = user?.name || "default name"; const name = user?.name ?? "default name"; ``` +## Object Merging: Spread Operator vs. Object.assign + +Avoid using the spread operator `({...obj})` in performance-critical situations where object merging occurs frequently (e.g., in loops or repeated function calls). The spread operator creates unnecessary intermediate objects, which can lead to higher memory usage and slower execution. + +Instead, consider using `Object.assign()` in these scenarios for better performance. The ESLint `prefer-object-spread` rule has been disabled to allow developers to make informed choices between readability and performance. + +- Use the spread operator for simple, small object merges where performance isn't a concern. +- Use Object.assign() when working with large objects or in performance-sensitive contexts. + +```ts +// BAD: Using the spread operator in a performance-critical loop +const errorSources = { + reportErrors, + ...reportErrorFields, + ...reportActionErrors, +}; + +// GOOD: Using Object.assign() for better performance in frequent merges +const errorSources = Object.assign({}, reportErrors, reportErrorFields, reportActionErrors); +``` + ## JSDocs - Omit comments that are redundant with TypeScript. Do not declare types in `@param` or `@return` blocks. Do not write `@implements`, `@enum`, `@private`, `@override`. eslint: [`jsdoc/no-types`](https://github.com/gajus/eslint-plugin-jsdoc/blob/main/.README/rules/no-types.md) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index afedd308371c..09cde50f8855 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -497,7 +497,6 @@ function getAllReportErrors(report: OnyxEntry, reportActions: OnyxEntry< } // All error objects related to the report. Each object in the sources contains error messages keyed by microtime // Use Object.assign to merge all error objects into one since it is faster and uses less memory than spread operator - // eslint-disable-next-line prefer-object-spread const errorSources = Object.assign({}, reportErrors, reportErrorFields, reportActionErrors); // Combine all error messages keyed by microtime into one object