for循环中的异步函数运行速度太慢。如何改善效果?

时间:2015-11-09 18:49:08

标签: javascript angularjs node.js google-maps

在我正在处理的应用程序中,我在Google地图上单击鼠标即可绘制一个部分。鼠标单击设置圆的中心。圆的半径是0.5英里。使用brandannee's node-gtfs时,我会进行异步调用以查找0.5英里范围内的路径。每个圈子通常有多条路线。然后,我为每个路线上最近的公交车站设置了一个标记,仅用于该圈内的一个方向(仅限于南行或北行)。问题是;它运作缓慢。我将findClosestStop函数的时间复杂度从n更改为log(n),希望它能产生显着差异。它有所不同但仍然需要4到10秒才能放置所有标记,这取决于路径附近的数量。这是我使用NodeJS和AngularJS的第一个项目。在这个项目之前,我只熟悉JavaScript的语法。我想知道我能做些什么才能让这项工作更快?我犯了一个概念错误吗?当我等待答案时,我会将(找到最接近的停止)功能移动到后端,看看它是否会对性能产生影响,即使我不这么认为,因为会有相同数量的计算。

如果您需要更多信息,请与我们联系。期待听到一些关于此的反馈。提前谢谢。

controller.js

    ...

        var distance = new distance.Distance();

    ...
        function createGTFSCluster(name, lat, lng, walk, bike, map) {
            var deferred = $q.defer();

            var clusterCenter = new graph.Center(lat, lng, map);
            var cluster = new graph.NodeCluster(clusterCenter, walkRadius, bikeRadius);

            cluster.setName(name);

            getRoutesNearby(cluster.clusterCenter, cluster.bikeRadius)
                .then(function(results) {
                    // Set the cluster's nearby routes
                    var routes = results[0];
                    cluster.nearbyRoutes = routes;
                    angular.forEach(routes, function(route, index){
                        getStopsByRoute(agency_key,route.route_id, 1).then(function(json){
                                            console.log(index, '->', route.route_id);
                            var stops = json[0];
                            var closestStop = distance.getClosestStop(stops, cluster);
                            cluster.setNodes(closestStop, route);
                        })
                    });
                        // Set the cluster's nodes to the stops found
                        deferred.resolve(cluster);
                });

            return deferred.promise;
        }
    ...


    // Retrieves the routes near a certain point on the map
        //
        // Input: cluster - A node cluster on the map we are finding routes for
        // Output: A promise whose results are the nearby route IDs
        function getRoutesNearby(center, radius) {
            var deferred = $q.defer();

            // Query the GTFS service for nearby routes and draw them
            gtfs.getRoutesByLocation(agency_key, center.lat, center.lon, radius)
                .then(function(json) {
                    //Get all route objects not only IDs
                    var routes = [];
                    for(index in json){
                        routes.push(json[index]);
                    }

                    // We got the routes, resolve
                    deferred.resolve(routes);
                });

            return deferred.promise;
        }
...

    function getStopsByRoute(agency_key, route_id, direction_id){
        var deferred = $q.defer();
        gtfs.getStopsByRoute(agency_key, route_id, direction_id)
            .then(function(json){ //all stops on the route in one direction
                var stopsArr =[];
                stopsArr.push(json.data[0].stops);
                deferred.resolve(stopsArr);
            });
            return deferred.promise;
    }

distance.js

    Distance.prototype.getClosestStop = function (stops, cluster){
      var closestStop = findClosestStop(stops, cluster);
      return closestStop;
    }

  function findDistance(stopObj, clusterObj){

    var stopCenter = {
      lat:stopObj.stop_lat,
      lon:stopObj.stop_lon
    };
    var clusterCenter = clusterObj.clusterCenter;

    var lat1 = stopCenter.lat;
    var lat2 = clusterCenter.lat;
    var lon1 = stopCenter.lon;
    var lon2 = clusterCenter.lon;
    var x1 = lat2 - lat1;
    var x2 = lon2 - lon1;
    var dLat = toRadians(x1);
    var dLon = toRadians(x2);
    var R = 3958.756; // miles
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                  Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
                  Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    return d;
  }


function toRadians(number){
  var inRadian = number * Math.PI / 180;
//  console.log('number->', number, 'inRadian->', inRadian);
  return inRadian;
}


     function findClosestStop(stops, cluster){
       //set new indeces
      var startIndex = 0;
      var endIndex = stops.length-1;

      //set new stop objects
      var firstStop = stops[startIndex];
      var lastStop = stops[endIndex];
      var closestStop;
      var returnIndex = 0;

      //dS: distance between the first stop and the cluster Center
      //dE: distance between the last stop and the cluster center
      var dS = findDistance(firstStop, cluster);
      var dE = findDistance(lastStop, cluster);
      if (dS > dE){
        startIndex = startIndex+endIndex / 2;
        returnIndex = 1;
      }
      if(dE > dS){
        endIndex = startIndex+endIndex / 2;
        returnIndex = 0;
      }

      if(stops.length > 2){
        stops = stops.slice(startIndex,(endIndex+1));
        return findClosestStop(stops, cluster);
      }else if(stops.length === 2){
        return stops[returnIndex];
      }else{
        return stops[0];
      }
    }

1 个答案:

答案 0 :(得分:2)

你在哪里浪费时间?

我建议使用一些

console.time(timerName);

看看你在哪里浪费时间。如果您确切知道丢失的位置,那么分析会更容易。

对于客户端应用程序来说,105组数据应该没有问题。

现在我建议您在控制台中运行这两个代码段

此代码段计算您的角度摘要。只需将其放入您的控制台,然后使用您的应用程序。

(function monitorDigestCycle(angular) {
  var injector = angular.element(document.body).injector();
  if (!injector) {
    throw new Error('Missing Angular injector on the document body');
  }
  var $rootScope = injector.get('$rootScope');
  function dummy() {
    console.count('digest cycle');
  }
  window.stopWatching = $rootScope.$watch(dummy);
  console.log('run window.stopWatching() to stop watching the digest cycle');
}(window.angular));

或者这个片段。它包装了一个函数(您需要调整自己使用的代码片段)并记录函数调用的chrome profiler。这样便于分析。

(function profileScopeMethod() {
  var selector = 'find';
  var methodName = 'find';
  var name = selector + ':' + methodName;

  /* global angular */
  var el = angular.element(document.getElementById(selector));
  var scope = el.scope() || el.isolateScope();
  console.assert(scope, 'cannot find scope from ' + name);

  var fn = scope[methodName];
  console.assert(typeof fn === 'function', 'missing ' + methodName);
  var $timeout = el.injector().get('$timeout');
  var $q = el.injector().get('$q');

  scope[methodName] = function () {
    console.profile(name);
    console.time(name);

    // method can return a value or a promise
    var returned = fn();
    $q.when(returned).finally(function finishedMethod() {
      console.timeStamp('finished', methodName);

      $timeout(function afterDOMUpdate() {
        console.timeStamp('dom updated after', methodName);
        console.timeEnd(name);
        console.profileEnd();
        scope[methodName] = fn;
        console.log('restored', name);
      }, 0);
    });
  };
  console.log('wrapped', name, 'for measurements');
}());

在这里,您可以找到更多代码片段来分析角度应用

https://github.com/bahmutov/code-snippets