Skip to content

Commit

Permalink
perf: improve assign ops/s by 2x
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinbaum committed Apr 5, 2024
1 parent 069b26c commit 8fa0e2b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
24 changes: 11 additions & 13 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,19 +748,17 @@ const set = (initial, path, value) => {
const assign = (initial, override) => {
if (!initial || !override)
return initial ?? override ?? {};
return Object.entries({ ...initial, ...override }).reduce(
(acc, [key, value]) => {
return {
...acc,
[key]: (() => {
if (isObject(initial[key]))
return assign(initial[key], value);
return value;
})()
};
},
{}
);
const merged = { ...initial };
for (const key in override) {
if (override.hasOwnProperty(key)) {
if (isObject(initial[key])) {
merged[key] = assign(initial[key], override[key]);
} else {
merged[key] = override[key];
}
}
}
return merged;
};
const keys = (value) => {
if (!value)
Expand Down
24 changes: 11 additions & 13 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,19 +751,17 @@ var radash = (function (exports) {
const assign = (initial, override) => {
if (!initial || !override)
return initial ?? override ?? {};
return Object.entries({ ...initial, ...override }).reduce(
(acc, [key, value]) => {
return {
...acc,
[key]: (() => {
if (isObject(initial[key]))
return assign(initial[key], value);
return value;
})()
};
},
{}
);
const merged = { ...initial };
for (const key in override) {
if (override.hasOwnProperty(key)) {
if (isObject(initial[key])) {
merged[key] = assign(initial[key], override[key]);
} else {
merged[key] = override[key];
}
}
}
return merged;
};
const keys = (value) => {
if (!value)
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "12.1.0",
"version": "12.1.1",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
25 changes: 13 additions & 12 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,20 @@ export const assign = <X extends Record<string | symbol | number, any>>(
): X => {
if (!initial || !override) return initial ?? override ?? {}

return Object.entries({ ...initial, ...override }).reduce(
(acc, [key, value]) => {
return {
...acc,
[key]: (() => {
if (isObject(initial[key])) return assign(initial[key], value)
// if (isArray(value)) return value.map(x => assign)
return value
})()
const merged = { ...initial }

for (const key in override) {
if(override.hasOwnProperty(key)) {
if(isObject(initial[key])) {
merged[key] = assign(initial[key], override[key])
}
},
{} as X
)
else {
merged[key] = override[key]
}
}
}

return merged;
}

/**
Expand Down

0 comments on commit 8fa0e2b

Please sign in to comment.