JavaScript-specific method for calculating the number of splits in binary 1 _ javascript skills

Source: Internet
Author: User
This is an online front-end pen question. The main idea is to use the toString method of JavaScript to convert the decimal number into a binary string. Then, for loop traversal calculates the number of times that "1" appears in the string. The Code is as follows:

The Code is as follows:


Function g (n ){
Var n = n. toString (2 );
Var count = 0;
For (var I = 0; I {
If (n [I] = "1 ")
Count ++;
}
Return count;
}


I think this writing is very troublesome. I suddenly thought that I could use the js split method to calculate the number of 1. The split parameter is regular \ 0 * \, which separates 1 in the string. The Code is as follows:

The Code is as follows:


Function f (n ){
Return n. toString (2). split (/0 */). length;
}


In this way, the code is very concise.

It is a pity that the efficiency of the two methods is tested. It is found that the efficiency of the regular split method is relatively low, and the time is about 2.5 times that of the for loop method.

<Script type = "text/javascript"> function f (n) {return n. toString (2 ). split (/0 */). length;} function g (n) {var n = n. toString (2); var count = 0; var tmp = ""; for (var I = 0; I <n. length; I ++) {if (n. charAt (I) = "1") count ++;} return count;} document. write ("split method calculation result:" + f (123456789987654321) + "<br>"); var now = Date. now (); for (var j = 0; j <10000; j ++) f (123456789987654321); document. write ("time spent by the split method (step 1):" + (Date. now ()-now) + "<br>"); document. write ("for Method Calculation Result:" + g (123456789987654321) + "<br>"); now = Date. now (); for (var k = 0; k <10000; k ++) g (123456789987654321); document. write ("for loop method time (step 1):" + (Date. now ()-now) + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Two errors are found in the original code.

First, in IE, strings cannot use array subscript to access the value at the specified position. They can only use charAt (index.

Second, in Chrome and Opera, the split (\ 0 * \) method calculates the number of 1 values, which may increase by 1 in some cases.

For example, if the binary value of 12 is 1100, the array generated by split (\ 0 * \) is [1, 1,]. That is to say, when the binary value does not end with 1, an empty array item will be generated at the end (this problem is not found in IE and Firefox ).

After thinking, we found that we do not need to use regular expressions to calculate the number of 1. We only need to use 1 as a parameter of the split method and 1 as a separator, the length of the split array should be 1 plus 1.

The Code is as follows:


Function f (n ){
Return n. toString (2). split ("1"). length-1;
}


In this way, the regular expression method is not used, and it is compatible with mainstream browsers, and its efficiency is not lower than the for Traversal method.

<Script type = "text/javascript"> function f (n) {return n. toString (2 ). split ("1 "). length-1;} function g (n) {var m = n. toString (2); var count = 0; for (var I = 0; I <m. length; I ++) {m. charAt (I )! = "1" | count ++;} return count;} document. write (123456789987654321 ). toString (2) + "<br>"); document. write ("for Method Calculation Result:" + g (123456789987654321) + "<br>"); var now = new Date (); for (var k = 0; k <10000; k ++) g (123456789987654321); document. write ("for loop method time (step 1):" + (new Date ()-now) + "<br>"); document. write ("split method calculation result:" + f (123456789987654321) + "<br>"); now = new Date (); for (var j = 0; j <10000; j ++) f (123456789987654321); document. write ("time spent by the split method (step 1):" + (new Date ()-now) + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

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.