Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 930aa4f

Browse files
perf(state): improve toDictionary & extract performance (#901)
1 parent ca6a614 commit 930aa4f

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

libs/state/spec/transformation-helpers/array/toDictionary.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ describe('toDictionary', () => {
6767
expect(dictionaryResult).toEqual(dictionaryByNumber);
6868
});
6969

70+
it('should change the reference', () => {
71+
const dictionaryResult = toDictionary(creatures, 'id');
72+
73+
expect(Object.values(dictionaryResult)[0] === creatures[0]).toBeFalsy();
74+
});
75+
7076
it('should create dictionary by string', () => {
7177
const dictionaryResult = toDictionary(creatures, 'type');
7278

libs/state/src/lib/transformation-helpers/array/extract.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isDefined, isKeyOf } from '../../core';
33
/**
44
* @description
55
* Accepts an array of objects of type T and single key or array of keys (K extends keyof T).
6-
* The `exctract` method is pure and immutable, thus not touching the input values and returning a shallow
6+
* The `exctract` method is pure and immutable, thus not touching the input values and returning a shallow
77
* copy of the extracted source.
88
*
99
* @example
@@ -34,7 +34,7 @@ import { isDefined, isKeyOf } from '../../core';
3434
* @docsPage slice
3535
* @docsCategory transformation-helpers
3636
*/
37-
export function extract<T extends object, K extends keyof T>(
37+
export function extract<T extends object, K extends keyof T>(
3838
array: T[],
3939
keys: K | K[]
4040
): Pick<T, K>[] {
@@ -46,15 +46,24 @@ export function extract<T extends object, K extends keyof T>(
4646
}
4747

4848
const sanitizedKeys = (Array.isArray(keys) ? keys : [keys]).filter(
49-
(k) => isKeyOf<T>(k) && array.some((i) => k in i)
49+
k => isKeyOf<T>(k) && array.some(i => k in i)
5050
);
51+
const length = sanitizedKeys.length;
5152

5253
if (!sanitizedKeys.length) {
5354
console.warn(`extract: provided keys not found`);
5455
return undefined as any;
5556
}
5657

57-
return array.map((i) =>
58-
sanitizedKeys.reduce((acc, k) => ({ ...acc, [k]: i[k] }), {} as Pick<T, K>)
58+
return array.map(item => {
59+
let i = 0;
60+
const result = {} as Pick<T, K>;
61+
62+
for(i; i < length; i++) {
63+
result[sanitizedKeys[i]] = item[sanitizedKeys[i]];
64+
}
65+
66+
return result;
67+
}
5968
);
6069
}

libs/state/src/lib/transformation-helpers/array/toDictionary.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ export function toDictionary<T extends object>(
6969
return {};
7070
}
7171

72-
return source.reduce(
73-
(acc, entity) => ({
74-
...acc,
75-
[entity[key] as any]: entity
76-
}),
77-
{}
78-
);
72+
const dictionary: { [key: string]: T } = {};
73+
const length = source.length;
74+
let i = 0;
75+
76+
for (i; i < length; i++) {
77+
dictionary[`${source[i][key]}`] = Object.assign({}, source[i]);
78+
}
79+
80+
return dictionary;
7981
}

0 commit comments

Comments
 (0)