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 )!