JS Loop traversal Condition collation optimization

Source: Internet
Author: User
Tags string indexof


[{"UUID": "3fadb39c-e19b-44af-846e-29ef6766433d", "url": "Http://xx.com/detail/ajax.do", "Time": "1409", "UA": " mozilla/5.0 (Linux; U Android 4.4.2; ZH-CN; Coolpad 8297 build/kot49h) applewebkit/537.36 (khtml, like Gecko) version/4.0 opr/11.0.0.98241 Mobile safari/537.36 "},{" UUID ":" Undefined "," url ":" Http://xx.com/ad/ajax.do "," Time ":" 298 "," ua ":" mozilla/5.0 (Linux; Android 4.3; ZH-CN; Yusun L63 build/jls36c) applewebkit/534.30 (khtml, like Gecko) version/4.0 opr/11.0.0.98306 safari/534.30 "},{" uuid ":" 3fadb39c-e19b-44af-846e-29ef6766433d "," url ":" Http://xx.com/ad/ajax.do "," Time ":" 1008 "," UA ":" mozilla/5.0 (Linux; U Android 4.4.2; ZH-CN; Coolpad 8297 build/kot49h) applewebkit/537.36 (khtml, like Gecko) version/4.0 opr/11.0.0.98241 Mobile safari/537.36 "},{" UUID ":" 970119c8-3b2c-4217-a6c0-234500f3c050 "," url ":" List "," Time ":" 187139 "," UA ":" mozilla/5.0 (Linux; U Android 4.4.2; ZH-CN; La5-w build/yusunla5-w) applewebkit/537.36 (khtml, like Gecko) version/4.0 opr/11.0.0.98306 Mobile safari/537.36 "},{" Uuid: "864d4099-e698-47b8-832b-a753e9c61e94", "url": "List", "Time": "584", "UA": "mozilla/5.0 (Linux; U Android 4.4.4; ZH-CN; OKA1 build/kot49h) applewebkit/537.36 (khtml, like Gecko) version/4.0 opr/11.0.0.98306 Mobile safari/537.36 "},{" uuid ":" 970119c8-3b2c-4217-a6c0-234500f3c050 "," url ":" Http://xx.com/ad/ajax.do "," Time ":" 689 "," UA ":" mozilla/5.0 (Linux; U Android 4.4.2; ZH-CN; La5-w build/yusunla5-w) applewebkit/537.36 (khtml, like Gecko) version/4.0 opr/11.0.0.98306 Mobile safari/537.36 "},...]

There are a large number of log data formats as above, where URLs have HTTP link detail/ajax,list/ajax, string "list", "detail", "beauty" in five cases.

Now we're going to divide the five categories in this data, the usual notation:

             for(; i < Len; i++) {                if(data[i][' uuid '] = = = ' undefined ') {Undefcount++Continue; }                if(data[i][' url ']=== ' list ') {List.push (data[i]); }Else if(data[i][' url ']=== ' detail ') {Detail.push (data[i]); }Else if(data[i][' url ']=== ' Beauty ') {Beauty.push (data[i]); }Else if(data[i][' url '].indexof (' list/ajax.do ') +1) {Listajax.push (data[i]); }Else if(data[i][' url '].indexof (' detail/ajax.do ') +1) {Detailajax.push (data[i]); }                            }

After writing it, it took 1000ms of time to find 11,000 data, and it felt a bit slow. Then think of the string indexof performance is very good, so change the loop:

   varUrlstr = ' listdetailbeautyhttp://xx.com/list/ajax.dohttp://xx.com/detail/ajax.do ';  for(; i < Len; i++) {                if(data[i][' uuid '] = = = ' undefined ') {Undefcount++Continue; }                Switch(Urlstr.indexof (data[i][' url ')])){                     Case0: List.push (Data[i]);  Break;  Case4: Detail.push (Data[i]);  Break;  Case10: Beauty.push (Data[i]);  Break;  Case16: Listajax.push (Data[i]);  Break;  Case42: Detailajax.push (Data[i]);  Break; default :                         Break; }            }

Constructs a urlstr string, then classifies through the indexof, the same data volume operation actually only needs 3ms, greatly improves the efficiency.

JS Loop traversal Condition collation optimization

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.