Skip to content

Commit 3271d3f

Browse files
LesiukDamian Lesiuk
and
Damian Lesiuk
authored
improve performance by not using spread operator #205 (#205)
Co-authored-by: Damian Lesiuk <[email protected]>
1 parent 48e77f6 commit 3271d3f

File tree

6 files changed

+132
-173
lines changed

6 files changed

+132
-173
lines changed

Diff for: cdn/radash.esm.js

+43-57
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ function zipToObject(keys, values) {
9898
return {};
9999
}
100100
const getValue = isFunction(values) ? values : isArray(values) ? (_k, i) => values[i] : (_k, _i) => values;
101-
return keys.reduce(
102-
(acc, key, idx) => ({ ...acc, [key]: getValue(key, idx) }),
103-
{}
104-
);
101+
return keys.reduce((acc, key, idx) => {
102+
acc[key] = getValue(key, idx);
103+
return acc;
104+
}, {});
105105
}
106106
const boil = (array, compareFunc) => {
107107
if (!array || (array.length ?? 0) === 0)
@@ -161,13 +161,10 @@ const replace = (list2, newItem, match) => {
161161
return [...list2];
162162
};
163163
const objectify = (array, getKey, getValue = (item) => item) => {
164-
return array.reduce(
165-
(acc, item) => ({
166-
...acc,
167-
[getKey(item)]: getValue(item)
168-
}),
169-
{}
170-
);
164+
return array.reduce((acc, item) => {
165+
acc[getKey(item)] = getValue(item);
166+
return acc;
167+
}, {});
171168
};
172169
const select = (array, mapper, condition) => {
173170
if (!array)
@@ -198,7 +195,8 @@ const unique = (array, toKey) => {
198195
const key = toKey ? toKey(item) : item;
199196
if (acc[key])
200197
return acc;
201-
return { ...acc, [key]: item };
198+
acc[key] = item;
199+
return acc;
202200
}, {});
203201
return Object.values(valueMap);
204202
};
@@ -217,20 +215,18 @@ const list = (startOrLength, end, valueOrMapper, step) => {
217215
};
218216
const flat = (lists) => {
219217
return lists.reduce((acc, list2) => {
220-
return [...acc, ...list2];
218+
acc.push(...list2);
219+
return acc;
221220
}, []);
222221
};
223222
const intersects = (listA, listB, identity) => {
224223
if (!listA || !listB)
225224
return false;
226225
const ident = identity ?? ((x) => x);
227-
const dictB = listB.reduce(
228-
(acc, item) => ({
229-
...acc,
230-
[ident(item)]: true
231-
}),
232-
{}
233-
);
226+
const dictB = listB.reduce((acc, item) => {
227+
acc[ident(item)] = true;
228+
return acc;
229+
}, {});
234230
return listA.some((value) => dictB[ident(value)]);
235231
};
236232
const fork = (list2, condition) => {
@@ -260,9 +256,10 @@ const merge = (root, others, matcher) => {
260256
return root.reduce((acc, r) => {
261257
const matched = others.find((o) => matcher(r) === matcher(o));
262258
if (matched)
263-
return [...acc, matched];
259+
acc.push(matched);
264260
else
265-
return [...acc, r];
261+
acc.push(r);
262+
return acc;
266263
}, []);
267264
};
268265
const replaceOrAppend = (list2, newItem, match) => {
@@ -317,13 +314,10 @@ const diff = (root, other, identity = (t) => t) => {
317314
return [...other];
318315
if (!other?.length)
319316
return [...root];
320-
const bKeys = other.reduce(
321-
(acc, item) => ({
322-
...acc,
323-
[identity(item)]: true
324-
}),
325-
{}
326-
);
317+
const bKeys = other.reduce((acc, item) => {
318+
acc[identity(item)] = true;
319+
return acc;
320+
}, {});
327321
return root.filter((a) => !bKeys[identity(a)]);
328322
};
329323
function shift(arr, n) {
@@ -567,52 +561,43 @@ const shake = (obj, filter = (x) => x === void 0) => {
567561
return keys.reduce((acc, key) => {
568562
if (filter(obj[key])) {
569563
return acc;
570-
} else
571-
return { ...acc, [key]: obj[key] };
564+
} else {
565+
acc[key] = obj[key];
566+
return acc;
567+
}
572568
}, {});
573569
};
574570
const mapKeys = (obj, mapFunc) => {
575571
const keys = Object.keys(obj);
576-
return keys.reduce(
577-
(acc, key) => ({
578-
...acc,
579-
[mapFunc(key, obj[key])]: obj[key]
580-
}),
581-
{}
582-
);
572+
return keys.reduce((acc, key) => {
573+
acc[mapFunc(key, obj[key])] = obj[key];
574+
return acc;
575+
}, {});
583576
};
584577
const mapValues = (obj, mapFunc) => {
585578
const keys = Object.keys(obj);
586-
return keys.reduce(
587-
(acc, key) => ({
588-
...acc,
589-
[key]: mapFunc(obj[key], key)
590-
}),
591-
{}
592-
);
579+
return keys.reduce((acc, key) => {
580+
acc[key] = mapFunc(obj[key], key);
581+
return acc;
582+
}, {});
593583
};
594584
const mapEntries = (obj, toEntry) => {
595585
if (!obj)
596586
return {};
597587
return Object.entries(obj).reduce((acc, [key, value]) => {
598588
const [newKey, newValue] = toEntry(key, value);
599-
return {
600-
...acc,
601-
[newKey]: newValue
602-
};
589+
acc[newKey] = newValue;
590+
return acc;
603591
}, {});
604592
};
605593
const invert = (obj) => {
606594
if (!obj)
607595
return {};
608596
const keys = Object.keys(obj);
609-
return keys.reduce(
610-
(acc, key) => ({
611-
...acc,
612-
[obj[key]]: key
613-
}),
614-
{}
615-
);
597+
return keys.reduce((acc, key) => {
598+
acc[obj[key]] = key;
599+
return acc;
600+
}, {});
616601
};
617602
const lowerize = (obj) => mapKeys(obj, (k) => k.toLowerCase());
618603
const upperize = (obj) => mapKeys(obj, (k) => k.toUpperCase());
@@ -636,7 +621,8 @@ const listify = (obj, toItem) => {
636621
if (entries.length === 0)
637622
return [];
638623
return entries.reduce((acc, entry) => {
639-
return [...acc, toItem(entry[0], entry[1])];
624+
acc.push(toItem(entry[0], entry[1]));
625+
return acc;
640626
}, []);
641627
};
642628
const pick = (obj, keys) => {

Diff for: cdn/radash.js

+43-57
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ var radash = (function (exports) {
101101
return {};
102102
}
103103
const getValue = isFunction(values) ? values : isArray(values) ? (_k, i) => values[i] : (_k, _i) => values;
104-
return keys.reduce(
105-
(acc, key, idx) => ({ ...acc, [key]: getValue(key, idx) }),
106-
{}
107-
);
104+
return keys.reduce((acc, key, idx) => {
105+
acc[key] = getValue(key, idx);
106+
return acc;
107+
}, {});
108108
}
109109
const boil = (array, compareFunc) => {
110110
if (!array || (array.length ?? 0) === 0)
@@ -164,13 +164,10 @@ var radash = (function (exports) {
164164
return [...list2];
165165
};
166166
const objectify = (array, getKey, getValue = (item) => item) => {
167-
return array.reduce(
168-
(acc, item) => ({
169-
...acc,
170-
[getKey(item)]: getValue(item)
171-
}),
172-
{}
173-
);
167+
return array.reduce((acc, item) => {
168+
acc[getKey(item)] = getValue(item);
169+
return acc;
170+
}, {});
174171
};
175172
const select = (array, mapper, condition) => {
176173
if (!array)
@@ -201,7 +198,8 @@ var radash = (function (exports) {
201198
const key = toKey ? toKey(item) : item;
202199
if (acc[key])
203200
return acc;
204-
return { ...acc, [key]: item };
201+
acc[key] = item;
202+
return acc;
205203
}, {});
206204
return Object.values(valueMap);
207205
};
@@ -220,20 +218,18 @@ var radash = (function (exports) {
220218
};
221219
const flat = (lists) => {
222220
return lists.reduce((acc, list2) => {
223-
return [...acc, ...list2];
221+
acc.push(...list2);
222+
return acc;
224223
}, []);
225224
};
226225
const intersects = (listA, listB, identity) => {
227226
if (!listA || !listB)
228227
return false;
229228
const ident = identity ?? ((x) => x);
230-
const dictB = listB.reduce(
231-
(acc, item) => ({
232-
...acc,
233-
[ident(item)]: true
234-
}),
235-
{}
236-
);
229+
const dictB = listB.reduce((acc, item) => {
230+
acc[ident(item)] = true;
231+
return acc;
232+
}, {});
237233
return listA.some((value) => dictB[ident(value)]);
238234
};
239235
const fork = (list2, condition) => {
@@ -263,9 +259,10 @@ var radash = (function (exports) {
263259
return root.reduce((acc, r) => {
264260
const matched = others.find((o) => matcher(r) === matcher(o));
265261
if (matched)
266-
return [...acc, matched];
262+
acc.push(matched);
267263
else
268-
return [...acc, r];
264+
acc.push(r);
265+
return acc;
269266
}, []);
270267
};
271268
const replaceOrAppend = (list2, newItem, match) => {
@@ -320,13 +317,10 @@ var radash = (function (exports) {
320317
return [...other];
321318
if (!other?.length)
322319
return [...root];
323-
const bKeys = other.reduce(
324-
(acc, item) => ({
325-
...acc,
326-
[identity(item)]: true
327-
}),
328-
{}
329-
);
320+
const bKeys = other.reduce((acc, item) => {
321+
acc[identity(item)] = true;
322+
return acc;
323+
}, {});
330324
return root.filter((a) => !bKeys[identity(a)]);
331325
};
332326
function shift(arr, n) {
@@ -570,52 +564,43 @@ var radash = (function (exports) {
570564
return keys.reduce((acc, key) => {
571565
if (filter(obj[key])) {
572566
return acc;
573-
} else
574-
return { ...acc, [key]: obj[key] };
567+
} else {
568+
acc[key] = obj[key];
569+
return acc;
570+
}
575571
}, {});
576572
};
577573
const mapKeys = (obj, mapFunc) => {
578574
const keys = Object.keys(obj);
579-
return keys.reduce(
580-
(acc, key) => ({
581-
...acc,
582-
[mapFunc(key, obj[key])]: obj[key]
583-
}),
584-
{}
585-
);
575+
return keys.reduce((acc, key) => {
576+
acc[mapFunc(key, obj[key])] = obj[key];
577+
return acc;
578+
}, {});
586579
};
587580
const mapValues = (obj, mapFunc) => {
588581
const keys = Object.keys(obj);
589-
return keys.reduce(
590-
(acc, key) => ({
591-
...acc,
592-
[key]: mapFunc(obj[key], key)
593-
}),
594-
{}
595-
);
582+
return keys.reduce((acc, key) => {
583+
acc[key] = mapFunc(obj[key], key);
584+
return acc;
585+
}, {});
596586
};
597587
const mapEntries = (obj, toEntry) => {
598588
if (!obj)
599589
return {};
600590
return Object.entries(obj).reduce((acc, [key, value]) => {
601591
const [newKey, newValue] = toEntry(key, value);
602-
return {
603-
...acc,
604-
[newKey]: newValue
605-
};
592+
acc[newKey] = newValue;
593+
return acc;
606594
}, {});
607595
};
608596
const invert = (obj) => {
609597
if (!obj)
610598
return {};
611599
const keys = Object.keys(obj);
612-
return keys.reduce(
613-
(acc, key) => ({
614-
...acc,
615-
[obj[key]]: key
616-
}),
617-
{}
618-
);
600+
return keys.reduce((acc, key) => {
601+
acc[obj[key]] = key;
602+
return acc;
603+
}, {});
619604
};
620605
const lowerize = (obj) => mapKeys(obj, (k) => k.toLowerCase());
621606
const upperize = (obj) => mapKeys(obj, (k) => k.toUpperCase());
@@ -639,7 +624,8 @@ var radash = (function (exports) {
639624
if (entries.length === 0)
640625
return [];
641626
return entries.reduce((acc, entry) => {
642-
return [...acc, toItem(entry[0], entry[1])];
627+
acc.push(toItem(entry[0], entry[1]));
628+
return acc;
643629
}, []);
644630
};
645631
const pick = (obj, keys) => {

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "10.3.1",
3+
"version": "10.3.2",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",
@@ -48,4 +48,4 @@
4848
"engines": {
4949
"node": ">=14.18.0"
5050
}
51-
}
51+
}

0 commit comments

Comments
 (0)