-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize reduce
loops - late 2023 edition
#168812
Changes from all commits
bf5dcf6
52d553e
ab7ac12
8f7d129
4b2c4fe
91f706d
30bb31a
35aeea4
7ebde8e
264c2db
605a66b
4b1067d
c71383a
7cc32bd
b2108b1
b30e195
18600e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,12 +77,14 @@ export const getEsQueryConfig = (params?: GetEsQueryConfigParamType): EsQueryCon | |
if (params == null) { | ||
return defaultConfigValues; | ||
} | ||
const paramKeysWithValues = Object.keys(params).reduce((acc: EsQueryConfig, key) => { | ||
const paramKeysWithValues = Object.keys(params).reduce((acc, key) => { | ||
const configKey = key as ConfigKeys; | ||
if (params[configKey] != null) { | ||
return { [key]: params[configKey], ...acc }; | ||
acc[key] = params[configKey]; | ||
} else { | ||
acc[key] = defaultConfigValues[configKey]; | ||
} | ||
return { [key]: defaultConfigValues[configKey], ...acc }; | ||
}, {} as EsQueryConfig); | ||
return paramKeysWithValues; | ||
return acc; | ||
}, {} as Record<string, unknown>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. castings like this in reduce can be avoided by passing interface in reduce generic const paramKeysWithValues = Object.keys(params).reduce<Record<string, unknown>>((acc, key) => { |
||
return paramKeysWithValues as EsQueryConfig; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,5 +58,8 @@ export async function asyncMapWithLimit<T1, T2>( | |
|
||
return results | ||
.sort(([a], [b]) => a - b) | ||
.reduce((acc: T2[], [, result]) => acc.concat(result), []); | ||
.reduce((acc: T2[], [, result]) => { | ||
acc.push(...result); | ||
return acc; | ||
}, []); | ||
Comment on lines
+61
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running a benchmark on NodeJS shows
This is the piece of code I ran: const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
const results = [
[0, ["a", "b", "c"]],
[2, ["g", "h", "i"]],
[1, ["d", "e", "f"]],
].sort(([a], [b]) => a - b);
suite
.add('flatMap', function () {
const out1 = results.flatMap(([, result]) => result);
})
.add('reduce with concat', function () {
const out1 = results.reduce((acc, [, result]) => acc.concat(result), []);
})
.add('reduce with push', function () {
const out1 = results.reduce((acc, [, result]) => {
acc.push(...result);
return acc;
}, []);
});
suite
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
});
suite.run(); |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a similar spread a few lines below. In the light of total elimination, should that be replaced by something like: