X Tutup
The Wayback Machine - https://web.archive.org/web/20211023024315/https://github.com/angular/angular/pull/39843
Skip to content
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

fix(core): not invoking object's toString when rendering to the DOM #39843

Closed

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Nov 25, 2020

Currently we convert objects to strings using '' + value which is quickest, but it stringifies the value using its valueOf, rather than toString. These changes switch to using String(value) which has identical performance and calls the toString method as expected. Note that another option was calling toString directly, but benchmarking showed it to be slower.

I've included the benchmark I used to verify the performance so we have it for future reference and we can reuse it when making changes to renderStringify in the future.

Also for reference, here are the results of the benchmark:

Benchmark: renderStringify
 concat: 2.006 ns(0%)
 concat with toString: 2.201 ns(-10%)
 toString: 237.494 ns(-11741%)
 toString with toString: 121.072 ns(-5937%)
 constructor: 2.201 ns(-10%)
 constructor with toString: 2.201 ns(-10%)
 toString mono: 14.536 ns(-625%)
 toString with toString mono: 9.757 ns(-386%)

Fixes #38839.

@google-cla google-cla bot added the cla: yes label Nov 25, 2020
@crisbeto crisbeto force-pushed the 38839/render-stringify-tostring branch 2 times, most recently from fbebcd5 to 9488506 Nov 25, 2020
@ngbot ngbot bot added this to the needsTriage milestone Nov 25, 2020
@ngbot ngbot bot added this to the needsTriage milestone Nov 25, 2020
@ngbot ngbot bot added this to the needsTriage milestone Nov 25, 2020
@crisbeto crisbeto marked this pull request as ready for review Nov 25, 2020
@pullapprove pullapprove bot requested a review from gkalpak Nov 25, 2020
@crisbeto crisbeto force-pushed the 38839/render-stringify-tostring branch from 9488506 to 29f855d Nov 28, 2020
Currently we convert objects to strings using `'' + value` which is quickest,
but it stringifies the value using its `valueOf`, rather than `toString`. These
changes switch to using `String(value)` which has identical performance
and calls the `toString` method as expected. Note that another option
was calling `toString` directly, but benchmarking showed it to be slower.

I've included the benchmark I used to verify the performance so we have it
for future reference and we can reuse it when making changes to `renderStringify`
in the future.

Also for reference, here are the results of the benchmark:

```
Benchmark: renderStringify
 concat: 2.006 ns(0%)
 concat with toString: 2.201 ns(-10%)
 toString: 237.494 ns(-11741%)
 toString with toString: 121.072 ns(-5937%)
 constructor: 2.201 ns(-10%)
 constructor with toString: 2.201 ns(-10%)
 toString mono: 14.536 ns(-625%)
 toString with toString mono: 9.757 ns(-386%)
```

Fixes angular#38839.
@crisbeto crisbeto force-pushed the 38839/render-stringify-tostring branch from 29f855d to 4893f2b Nov 28, 2020
@crisbeto crisbeto requested a review from mhevery Nov 30, 2020
@mhevery mhevery self-assigned this Nov 30, 2020
@mhevery
Copy link
Contributor

@mhevery mhevery commented Nov 30, 2020

@mhevery
Copy link
Contributor

@mhevery mhevery commented Nov 30, 2020

@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Nov 30, 2020

FYI, adding "risk: medium" to indicate that it might make sense to land it as a separate CL in g3.

Copy link
Contributor

@mhevery mhevery left a comment

reviewed-for: global-approvers

@mhevery mhevery removed the request for review from gkalpak Nov 30, 2020
jessicajaniuk added a commit that referenced this issue Nov 30, 2020
…39843)

Currently we convert objects to strings using `'' + value` which is quickest,
but it stringifies the value using its `valueOf`, rather than `toString`. These
changes switch to using `String(value)` which has identical performance
and calls the `toString` method as expected. Note that another option
was calling `toString` directly, but benchmarking showed it to be slower.

I've included the benchmark I used to verify the performance so we have it
for future reference and we can reuse it when making changes to `renderStringify`
in the future.

Also for reference, here are the results of the benchmark:

```
Benchmark: renderStringify
 concat: 2.006 ns(0%)
 concat with toString: 2.201 ns(-10%)
 toString: 237.494 ns(-11741%)
 toString with toString: 121.072 ns(-5937%)
 constructor: 2.201 ns(-10%)
 constructor with toString: 2.201 ns(-10%)
 toString mono: 14.536 ns(-625%)
 toString with toString mono: 9.757 ns(-386%)
```

Fixes #38839.

PR Close #39843
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Dec 31, 2020

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 31, 2020
@pullapprove pullapprove bot removed the comp: core label Dec 31, 2020
@ngbot ngbot bot removed this from the needsTriage milestone Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
X Tutup