Home >Web Front-end >JS Tutorial >Performance optimization of type conversion in javascript_javascript skills
1. Convert numbers into strings and use "" 1. Although it looks a bit uglier, in fact this is the most efficient, in terms of performance: ("" ) > String() > .toString() > new String (), try to use internal operations that can be used at compile time to be faster than user operations used at runtime. String() is an internal function, so it is very fast, while .toString() needs to query the function in the prototype, so it is not as fast. new String() is used to return an exact copy.
2. Converting floating point numbers to integers is more error-prone. Many people like to use parseInt(). In fact, parseInt() is used to convert strings into numbers, not between floating point numbers and integers. To convert between, we should use Math.floor() or Math.round(). In addition, unlike the problem in object search in Section 2, Math is an internal object, so Math.floor() actually does not have many query methods and calling times, and is the fastest.
3. For custom objects, if the toString() method is defined for type conversion, it is recommended to explicitly call toString(), because the internal operation will try the object after trying all possibilities. The toString() method tries to see if it can be converted.