javascript - nodejs 优化问题

查看:116
本文介绍了javascript - nodejs 优化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如果我需要用nodejs 请求多个java的接口最后得到所有数据渲染页面,怎么样能提高速度
之前我是这样的

 //请求接口
  http.tp(options,function(error,response,tags){
        http.tp(options1,function(error,response,topic){
            http.tp(follow,function(error,response,follow){
                http.tp(options2,function(error,response,topicRCMD){
                    data={
                        follow:follow,
                        topicRCMD:topicRCMD,
                        tags:tags,
                        topicHot:topic,
                    }
                    opt.render(data);  //渲染页面
                })
            })
        })
    })

这样速度很慢。然后我引用async

return async.parallel({
        //我关注的人
        follow:function(callback){
            http.tp(follow,function(error, response, follow){
                callback(null, follow);
            })
        },
        // 获取标签
        tags:function(callback){
            http.tp(options,function(error, response, tags){
                callback(null, tags);
            })
        },
        // 获取热门话题
        topicHot:function(callback){
            http.tp(options1,function(error, response, topicHot){
                callback(null, topicHot);
            })
        },
        // 获取推荐话题列表
        topicRCMD:function(callback){
            http.tp(options2,function(error, response, topicRCMD){
                callback(null, topicRCMD);
            })
        }
    },
    function(err, results){
        console.log('ssss:',results);
        opt.render(results); //渲染页面
    });

这样子速度变快了点,但还是很慢,请问这个有什么好的解决办法吗

解决方案

第一个方法慢是因为需要花费的时间是所有请求时间的总和;第二种方法只需要花费最长的那个请求需要的时间,自然会快一些
再想优化了就需要在渲染过程或者服务器端请求处理过程上优化了

这篇关于javascript - nodejs 优化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆