我有一个非常简单的资源来调用CakePHP。当我使用Advanced客户端运行请求时,它返回的是大约100 in。在角度上它需要2-4秒。下面是我正在运行的代码,以“证明”资源是瓶颈。
资源:
var resource = $resource('/index.php/props/:op/:id.json', {}, {
getPropertyListByCategory: { method:'GET', params:{ op:'getPropertyListByCategory', category:'@category' } },
setPropertyListByCategory: { method:'POST', params:{ op:'setPropertyListByCategory' } }
});方法:
function getPropertyListByCategory(params) {
var x1 = Date.now();
var deferred = $q.defer();
resource.getPropertyListByCategory(params, function(resp) {
var x2 = Date.now();
console.log(x2-x1);
deferred.resolve(resp.data);
});
return deferred.promise;
}您可以在资源调用之前看到I时间戳,然后将其与解析进行比较。同样,当我直接针对Cake运行此请求时,它返回的速度非常快。蛋糕不是问题。
有什么办法让这件事加速一点吗?
发布于 2014-01-28 16:22:53
$resource并不慢
我想把它写在评论里,但太长了,所以.
人们可能会认为,$resource是这里的瓶颈,但我认为这是因为您不完全了解异步代码是如何工作的,更不能理解它是如何工作的。
您的"benchmark"无效,它假定会立即调用回调,并且之间不会运行其他代码。
参见这个问题:AngularJS: Why does $http not actually make a http request until you leave the current digest?
$resource使用$http,它在执行下一个$digest()之前实际上不会发送请求。
很多事情都会使$digest变慢,您应该检查应用程序的其他部分。
您还可以创建一个只有$resource的角度测试应用程序,看看它是否仍然很慢。
如果您想一想,您最后一次听说不涉及angular.js性能问题是什么时候?!
再说一遍,$resource有什么慢的?!这只是一个简单的服务。
function getPropertyListByCategory(params) {
var x1 = Date.now();
var deferred = $q.defer();
// the request is queued and will run after a $digest!!
resource.getPropertyListByCategory(params, function(resp) {
var x2 = Date.now();
deferred.resolve(resp.data);
});
// x3 is assigned before x2 ..
var x3 = Date.now();
return deferred.promise;
}https://stackoverflow.com/questions/21410434
复制相似问题