Home > Web Front-end > JS Tutorial > body text

Performance optimization of type conversion in javascript_javascript skills

WBOY
Release: 2016-05-16 18:13:52
Original
1006 people have browsed it

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.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template