[英]iterating associative arrays taking too long
I am having this javascript object which contains city list along with dependency details on state. 我有这个javascript对象,其中包含城市列表以及州的依存关系详细信息。
var cityList = {
'1' : ['CITY_CODE_A','City A', '2','11','2'],
'2' : ['CITY_CODE_B','City B','','11',''],
'3' : ['CITY_CODE_C','City C', '','11','']
};
var stateList = {
'11' : ['STATE_CODE_A', 'State A'],
'12' : ['STATE_CODE_B', 'State B'],
'13' : ['STATE_CODE_C', 'State C'],
'14' : ['STATE_CODE_D', 'State D']
};
Depending on the state selected, key is passed to a function which matches the key with the values (index 3 element in array) in the city list and if found forms an option tag of <select>
. 根据所选择的状态,将键传递给将键与城市列表中的值(数组中的索引3元素)相匹配的函数,如果找到该键,则形成选项标签
<select>
。 Here is the code : 这是代码:
$.each(cityList, function(key, value) {
if(selectedVal == value[3]){
$("select[id='"+selectid+"']").append('<option value="'+value[0]+'" code="'+key+'">' +value[1] + '</option>');
}
});
Now when this city list grows to 50K records (2 MB) wiz a state has 50K cities (performance testing) the above code takes 2 mins to load the city list dropdown on Windows 7 machine running the code locally. 现在,当该城市列表增长到5万条记录(2 MB)时,一个州有5万个城市(性能测试),以上代码需要2分钟才能在本地运行该代码的Windows 7计算机上加载城市列表下拉列表。 Tried with native
for
/ in
-loop but did not get satisfactory outcome. for
/ in
loop中尝试使用native for
但未获得满意的结果。
Any ideas on how to reduce the time ? 有什么想法可以减少时间吗? Do I need to change the JS object structure?
我是否需要更改JS对象结构?
I am also a friend of better use of search instead of 50k scrolldown items. 我也是更好地使用搜索而不是5万个向下滚动项的朋友。 But to answer your question I would improve your code by using:
但是要回答您的问题,我将使用以下方法来改进您的代码:
var html = '';
$.each(cityList, function (key, value) {
html += '<option value="' + value[0] + '" code="' + key + '">' + value[1] + '</option>'
});
$("select[id='1']").append(html);
This will speed up your option
creation a lot because the DOM will only be changed one time, not 50k times like you said in your example. 这将大大加快您的
option
创建速度,因为DOM仅会更改一次,而不是您在示例中所说的50k次。 ( Short Demo ) ( 简短演示 )
Edit: @downvoters, could you explain why you think it's wrong? 编辑:@downvoters,您能解释一下为什么您认为这是错误的吗? But first try running this test .
但是首先尝试运行此测试 。
If you want to get cities for a particular state you could create a map where state IDs are the key, 如果要获取特定州的城市,可以创建一个以州ID为关键的地图,
var cityList = {
11 : [ ['CITY_CODE_A','City A', '2','11','2'], ['CITY_CODE_B','City B','','11',''] ],
12 : [ ['CITY_CODE_C','City C', '2','12','2'], ['CITY_CODE_D','City D','','12',''] ]
}
Now you only need to iterate over citylist[ selectedVal ]
现在您只需要遍历
citylist[ selectedVal ]
$.each(cityList[selectedVal], function(key, value) {
$("select[id='"+selectid+"']").append('<option value="'+value[0]+'" code="'+key+'">' +value[1] + '</option>');
});
To optimize it further you should avoid DOM manipulation (adding <option>
elements) in a loop, and buffer it instead: 为了进一步优化它,您应该避免在循环中进行DOM操作(添加
<option>
元素),并对其进行缓冲:
var $selectEl = $('<select />');
$.each(cityList[selectedVal], function(key, value) {
$selectEl.append('<option value="'+value[0]+'" code="'+key+'">' +value[1] + '</option>');
});
$selectEl.attr( 'id', selectid );
$("select[id='"+selectid+"']").replaceWith( $selectEl );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.