JavaScript-specific method computing the number of 1 in binary split method _javascript technique

Source: Internet
Author: User
The code is as follows:
Copy Code code as follows:

function g (n) {
var n = n.tostring (2);
var count = 0;
for (Var i=0;i<n.length;i++)
{
if (n[i] = = "1")
count++;
}
return count;
}

Think this write is very troublesome, suddenly think is not can use JS split method to achieve 1 of the number, split parameter is regular \0*\, separate string of 1. The code is as follows:
Copy Code code as follows:

function f (n) {
Return n.tostring (2). Split (/0*/). length;
}

So the code seems very concise.

Unfortunately, the efficiency of the two methods was tested, and it was found that using the regular split method was less efficient and time was about 2.5 times times that of the For loop method.
<textarea id="runcode75703"><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 ("The split method calculates the result:" +f (123456789987654321) + "<br>"); var now = Date.now (); for (Var j=0;j<10000;j++) f (123456789987654321); document.write ("The Split method takes time (10000 steps):" + (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 (10000 steps): "+ (Date.now ()-now) + <br>"); </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

two errors were found in the original code.

One is under IE, the string cannot use the array subscript to access the value of the specified position, only the Charat (index) method can be used.

Second, in Chrome and opera, the split (\0*\) way to calculate the number of 1 in some cases will be more than 1.

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

After thinking that, in fact, do not need to use the regular way to calculate the number of 1, as long as the 1 as the parameters of the split method, 1 as a separator, the length of the array should be 1 of the number plus 1.
Copy Code code as follows:

function f (n) {
Return n.tostring (2). Split ("1"). length–1;
}

This will not take the regular approach, but also compatible with the mainstream browsers, and its efficiency is no less than using for traversal method.
<textarea id="runcode67417"><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><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 (10000 steps) for the Loop method: + (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 ("The time taken by the Split method (10000 steps):" + (New Date ()-now) + "<br>"); </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.