question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warn if stringifying an attribute takes too long

See original GitHub issue

With React 16 we don’t have an attribute whitelist so both src={obj} and myattribute={obj} would be valid. The objects get stringified and added as attributes for smooth migration path because a lot of the existing code already depends on this behavior.

There is, however, one pitfall here. Sometimes you made do <div {...rest}> and not realize that rest includes an object whose stringifying is unusually expensive. For example a deeply nested Immutable Map. Now, this wouldn’t produce an error, but it would slow down rendering for no good reason.

We could protect against this by putting performance.now() counters around the places where we stringify attributes. If stringification takes more than, say, 2 milliseconds, then something bad is going on, and we should probably warn.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
awearycommented, Feb 12, 2018

@Swieckowski I think you’ll want to look at setValueForProperty in DOMPropertyOperations

https://github.com/facebook/react/blob/fb85cf2e9c52a4b2999144a89a6ee8256aec55c7/packages/react-dom/src/client/DOMPropertyOperations.js#L164-L166

https://github.com/facebook/react/blob/fb85cf2e9c52a4b2999144a89a6ee8256aec55c7/packages/react-dom/src/client/DOMPropertyOperations.js#L136

Those two lines are where known attribute and unknown attribute values get stringified, respectively. I’m not sure if @gaearon intends to only warn for attributes that we don’t recognize, but this should be a good place to start.

Keep in mind that this warning and timing logic should only happen in the DEV bundle. This is handled by wrapping code in __DEV__ checks, which you can see examples of in the file linked above.

Hope that helps!

1reaction
awearycommented, Mar 27, 2018

@wuweiweiwu it stalled because we didn’t provide any feedback. I just requested some changes, so let’s give @Swieckowski some time to address those requests before handing it off 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it bad to add JSON on HTML data attribute? - Stack Overflow
While there's nothing to stop you embedding a long string of JSON in a data attribute, arguably the more "correct" way of doing...
Read more >
JavaScript JSON stringify() Method - W3Schools
The JSON.stringify() method converts JavaScript objects into strings. When sending data to a web server the data has to be a string.
Read more >
JSON methods, toJSON - The Modern JavaScript Tutorial
So it's easy to use JSON for data exchange when the client uses ... The method JSON.stringify(student) takes the object and converts it...
Read more >
5 Ways to Log an Object to the Console in JavaScript
“The JSON.stringify() method converts a JavaScript object or ... This is super useful if you need to substitute alert() for console.log() .
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found