Benchmark of the efficient Javscript string replacement function

Source: Internet
Author: User
This is a frequently used special character replacement function for HTML. Replace & amp;, & lt;, & gt;, & quot; and other functions with & amp;, & lt;, & gt;, & quot ;. The general practice is to use several replace functions consecutively, while Clear only uses one replace to complete the replacement. This is a frequently used function for replacing special HTML characters. functions such as &, <,>, and "are replaced with &, <,>, and ,". The general practice is to use several replace functions consecutively, while Clear only uses one replace to complete the replacement.

I made a benchmark on these two functions and found that the efficiency of the new functions is about 1.4 times that of the original functions. Although it is not as efficient as expected, the efficiency should be improved when there are more strings to be replaced.

The test method is to execute two functions 2048 times for a 100-byte string, and obtain the execution time. The following are the results of 10 tests. ToTXT_1 is the common practice, and toTXT_2 is the Clear method.

ToTXT_1 (MS) ToTXT_2 (MS) ToTXT_1/toTXT_2
1609 1204 75%
2250 1547 69%
1672 1313 79%
1813 1250 69%
1844 1219 66%
2063 1421 69%
2031 1172 58%
1797 1360 76%
2015 1281 64%
1860 1500 81%

The above is the benchmark of the efficient Javscript string replacement function. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.