js循环遍历条件归类优化


[{"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"},...]

  现有大量log数据格式如上,其中url有http链接detail/ajax,list/ajax,字符串"list","detail","beauty"这五种情况。

现在要再这万条数据中把这五类分出来,常规写法:

            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]);
                }
                
            }

写完之后发现11000条数据要花费1000ms的时间,感觉有点慢了。遂想到字符串的indexOf的性能非常好,于是改造一下循环:

   var urlStr = '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'])){
                    case 0:
                        list.push(data[i]);
                        break;
                    case 4:
                        detail.push(data[i]);
                        break;
                    case 10:
                        beauty.push(data[i]);
                        break;
                    case 16:
                        listAjax.push(data[i]);
                        break;
                    case 42:
                        detailAjax.push(data[i]);
                        break;
                    default :
                        break;
                }

            }

构造一个urlStr的字符串,然后通过indexOf进行分类,同样的数据量运行居然只需要3ms,极大提升了效率。

posted @ 2015-12-18 16:33  Tony_M  阅读(1180)  评论(0编辑  收藏  举报