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

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 Compare November 25, 2020 20:08
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release type: bug/fix labels Nov 25, 2020
@ngbot ngbot bot modified the milestone: needsTriage Nov 25, 2020
@crisbeto crisbeto marked this pull request as ready for review November 25, 2020 20:34
@pullapprove pullapprove bot requested a review from gkalpak November 25, 2020 20:34
@crisbeto crisbeto force-pushed the 38839/render-stringify-tostring branch from 9488506 to 29f855d Compare November 28, 2020 10:18
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 Compare November 28, 2020 11:02
@mhevery mhevery self-assigned this Nov 30, 2020
@mhevery
Copy link
Contributor

mhevery commented Nov 30, 2020

presubmit

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 30, 2020
@mhevery
Copy link
Contributor

mhevery commented Nov 30, 2020

presubmit

@AndrewKushnir
Copy link
Contributor

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: global-approvers

@mhevery mhevery removed the request for review from gkalpak November 30, 2020 20:17
jessicajaniuk pushed a commit that referenced this pull request 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

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 area: core Issues related to the framework runtime 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.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes risk: medium target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Angular Ivy causes objects not rendering correctly when implemented toString and valueOf
3 participants