Created by: denyo
Based on the discussion and feedback in #4159 (closed) I spent some time testing different approaches to compose the HttpQuery
parameters.
Of course using if
checks and manipulating the object over spreading an expression is a lot more performant. Further improvements can be achieved by destrucuring the parameters.
Results of the tests can be found here
- https://jsperf.com/object-creation-or-spreadoperator-extended with 6/6 parameters
- https://jsperf.com/object-creation-or-spreadoperator-extended-2 with 2/6 parameters
Since the pet store example is a bit limited here are some before and afters from another project:
- throwIfRequired(requestParameters, 'skillIds', 'deleteMySkillsUsingDELETE');
+ throwIfNullOrUndefined(skillIds, 'deleteMySkillsUsingDELETE');
- const query: HttpQuery = {
- ...(requestParameters.skillIds && { 'skillIds': requestParameters.skillIds }),
+ const query: HttpQuery = { // required parameters are used directly since they are already checked by throwIfNullOrUndefined
+ 'skillIds': skillIds,
};
- const query: HttpQuery = {
- ...(requestParameters.page && { 'page': requestParameters.page }),
- ...(requestParameters.size && { 'size': requestParameters.size }),
- ...(requestParameters.skillIds && { 'skillIds': requestParameters.skillIds }),
- };
+ const query: HttpQuery = {};
+
+ if (page != null) { query['page'] = page; }
+ if (size != null) { query['size'] = size; }
+ if (skillIds != null) { query['skillIds'] = skillIds; }
- const query: HttpQuery = {
- ...(requestParameters.keyword && { 'keyword': requestParameters.keyword }),
- ...(requestParameters.lang && { 'lang': requestParameters.lang }),
+ const query: HttpQuery = { // required parameters are used directly since they are already checked by throwIfNullOrUndefined
+ 'keyword': keyword,
};
+ if (lang != null) { query['lang'] = lang; }
Sizewise after bundeling one of my projects with CRA2 the main chunk went from 344 KB to 340 KB. After gzip it's about 0.1 KB less. When looking at the character count it went from 343498 to 339245. That's not huge but still.
In Summary:
- fixed a bug where parameters like
false
and0
were lost since they were evaluated with&&
inside the expression - improved performance
- less redundancy
- smaller bundle
-
throwIfRequired
is marked as deprecated and unused within the generated code
PR checklist
-
Read the contribution guidelines. -
If contributing template-only or documentation-only changes which will change sample output, build the project before. -
Run the shell script(s) under ./bin/
(or Windows batch scripts under.\bin\windows
) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the code or mustache templates for a language ({LANG}
) (e.g. php, ruby, python, etc). -
File the PR against the correct branch: master
,4.1.x
,5.0.x
. Default:master
. -
Copy the technical committee to review the pull request if your PR is targeting a particular programming language.
@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) @akehir (2019/07)