Skip to content

Commit

Permalink
perf: clone only once
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 12, 2024
1 parent 72ccc7d commit ffb92fd
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,12 +1110,11 @@ type MergeWithDefaultsResult<D, V> =
: MaybeFallback<D, V>
: MaybeFallback<D, V>

export function mergeWithDefaults<
function mergeWithDefaultsRecursively<
D extends Record<string, any>,
V extends Record<string, any>,
>(defaults: D, values: V): MergeWithDefaultsResult<D, V> {
// NOTE: we need to clone the value here to avoid mutating the defaults
const merged: Record<string, any> = deepClone(defaults)
const merged: Record<string, any> = defaults
for (const key in values) {
const value = values[key]
// let null to set the value (e.g. `server.watch: null`)
Expand All @@ -1138,6 +1137,15 @@ export function mergeWithDefaults<
return merged as MergeWithDefaultsResult<D, V>
}

export function mergeWithDefaults<
D extends Record<string, any>,
V extends Record<string, any>,
>(defaults: D, values: V): MergeWithDefaultsResult<D, V> {
// NOTE: we need to clone the value here to avoid mutating the defaults
const clonedDefaults = deepClone(defaults)
return mergeWithDefaultsRecursively(clonedDefaults, values)
}

function mergeConfigRecursively(
defaults: Record<string, any>,
overrides: Record<string, any>,
Expand Down

0 comments on commit ffb92fd

Please sign in to comment.