SizeFormatHelpers: defend against invalid sizes#6905
SizeFormatHelpers: defend against invalid sizes#6905sokra merged 1 commit intowebpack:masterfrom xtuc:fix-handle-unknown-size
Conversation
| const SizeFormatHelpers = exports; | ||
|
|
||
| SizeFormatHelpers.formatSize = size => { | ||
| if (typeof size !== "number" || Number.isNaN(size) === true) { |
There was a problem hiding this comment.
What do you mean?
That's a more explicit check otherwise the engine has to run the ToBoolean coercion.
There was a problem hiding this comment.
Number.isNaN already returns a boolean. There is no benefit, performance-wise nor readability-wise (granted, readability is subjective but I'd argue it makes it less readable), to add === true behind.
In fact as far as performance micro-optimization goes. I bet if(Number.isNaN(size) === true) is actually slightly slower than if(Number.isNaN(size))
There was a problem hiding this comment.
I would leave comparison there, but after searching codebase there is someplaces where it is not used:
Line 26 in fde0183
There was a problem hiding this comment.
To me, if (Number.isNaN(size)) mentally parses as "if "size" is NaN then" and if (Number.isNaN(size) === true) parses as "if ("size" is NaN) equals true then". That's why the former is more readable. Airbnb styleguide seems to agree with that, couldn't find any sources that defends the latter.
Also did a little performance benchmark and can confirm that the former is faster.
It's ofcourse only a minor issue and in part a matter of personal preference.
|
Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon. |
|
It looks like this Pull Request doesn't include enough test cases (based on Code Coverage analysis of the PR diff). A PR need to be covered by tests if you add a new feature (we want to make sure that your feature is working) or if you fix a bug (we want to make sure that we don't run into a regression in future). @xtuc Please check if this is appliable to your PR and if you can add more test cases. Read the test readme for details how to write test cases. |
|
Thanks |
What kind of change does this PR introduce?
Avoids having
NaN undefinedin the chunk size.before:
after:
and webpack/webpack-sources#38 fixes the wasm unknown size.
Did you add tests for your changes?
yes 👍
If relevant, link to documentation update:
Summary
Does this PR introduce a breaking change?
no 👍
Other information