javascript - Very slow loop in angularjs -
i use loop create map markers (1000 points):
var markers = {}; (var = 0; < items.data.data.length; i++) { latval = items.data.data[i].lat; lngval = items.data.data[i].lng; ikona = icons.infost; message = "<b>" + items.data.data[i].name + "</b>"; markers[i] = {'group': 'cmp', 'lat' : eval(latval), 'lng' : eval(lngval), 'icon' : ikona, 'message' : message}; } $scope.markers = markers;
how can improve loop speed in angularjs (currently takes 10s)?
there few things can improve code speed avoiding eval
, caching loop variables , arrays well. caching values, reduce bunch of operations member access , unecessary calculations.
var items = { data: { data: [] }}; var icons = { infost: 'infost'}; (var = 0; < 1000; i++) { items.data.data.push({ lat: ''+i, lng: ''+i, name:''+i }); } console.time('time'); /// code starts here var data = items.data.data, l = data.length, markers = array(l), // or [] item, latval, lngval, ikona, message; (var = 0; < l; i++) { item = data[i]; latval = item.lat; lngval = item.lng; ikona = icons.infost; message = "<b>" + item.name + "</b>"; markers[i] = { group: 'cmp', lat: +latval, lng: +lngval, icon: ikona, message: message }; } console.timeend('time'); //$scope.markers = markers;
Comments
Post a Comment