Coffeescript隐含的回报对性能和副作用产生影响(Coffeescript implicit returns impact on performance and side effects)
我目前正在开发一个使用Express.js和Mongoose的node.js Web服务。 最近,我想我会尝试使用CoffeeScript,因为我听说有一些好处。 但是,我注意到了一些有点令人不安的事情,我很好奇是否有人可以澄清。
这是我在普通javascript中的一条路线:
router.post('/get/:id', decorators.isManager, function(req, res) { Group.findById(req.params.id, function(err, grp) { if(err) res.status(500).end(); if(grp == null) res.status(500).send('The group could not be found'); res.send(grp); }); });
但是,使用以下(几乎相当于coffeescript):
router.post '/get/:id', decorators.isManager, (req, res) -> Group.findById req.params.id, (err, grp) -> res.status(500).end() if err res.status(500).send 'The group could not be found' if not grp? res.send grp
将此重新编译为javascript会返回以下内容:
router.post('/get/:id', decorators.isManager, function(req, res) { return Group.findById(req.params.id, function(err, grp) { if(err) res.status(500).end(); if(grp == null) res.status(500).send('The group could not be found'); return res.send(grp); }); });
这些额外的回报会影响我的应用程序的性能吗,它会改变它的工作方式吗? 我测试了它,似乎是相同的响应时间但是我不确定这会对多个嵌套查询产生什么影响。 谢谢!
I am currently working on a node.js web service which utilizes Express.js and Mongoose. Recently, I figured I would try my hand at CoffeeScript, as I have heard that there are some benefits to be had. However, I have noticed something a little unsettling, and I was curious if someone could clarify.
Here is one of my routes in plain javascript:
router.post('/get/:id', decorators.isManager, function(req, res) { Group.findById(req.params.id, function(err, grp) { if(err) res.status(500).end(); if(grp == null) res.status(500).send('The group could not be found'); res.send(grp); }); });
However, with the following (nearly equivalent coffeescript):
router.post '/get/:id', decorators.isManager, (req, res) -> Group.findById req.params.id, (err, grp) -> res.status(500).end() if err res.status(500).send 'The group could not be found' if not grp? res.send grp
Re-compiling this to javascript returns the following:
router.post('/get/:id', decorators.isManager, function(req, res) { return Group.findById(req.params.id, function(err, grp) { if(err) res.status(500).end(); if(grp == null) res.status(500).send('The group could not be found'); return res.send(grp); }); });
Will those extra returns effect the performance of my application, does it alter the way it works? I tested it, it seems to be the same response time however I am not sure the impact this will have on multiple nested queries. Thank you!
原文:https://stackoverflow.com/questions/29070113
满意答案
将字符串文字传递给
getURI
方法:Api.getURI('outline')
调用
Api.getURI(outline)
使解释器查找outline
变量,这在当前作用域中是未定义的(因此是ReferenceError)。Pass string literal to
getURI
method:Api.getURI('outline')
Calling
Api.getURI(outline)
makes interpreter look foroutline
variable, which is undefined in the current scope (hence ReferenceError).Protip: linter, like ESLint, would catch this error early.
相关问答
更多ReactJS API数据获取CORS错误(ReactJS API Data Fetching CORS error)
ReactJS componentDidMount和Fetch API(ReactJS componentDidMount and Fetch API)
如何将List 传递给api url?(How can I pass a List to an api url?)
从ReactJs中提取API中的数据(Extract data from API in ReactJs)
如何将道具传递给reactjs处理程序?(How to pass Props to reactjs handler?)
将多个参数传递给后端API reactjs(Pass multiple argument to back-end API reactjs)
如何从Reactjs调用Web服务(How to call the webservice from Reactjs)
如何将API从API传递给Action(ReactJS)(How pass URL from API to Action (ReactJS))
Django在一个像+ reactjs的API中。(Django in an API like + reactjs. How to generate a csrf token)
传递给WEb API Action时,在JSON中丢失值(Losing values in JSON when passing to WEb API Action)
相关文章
更多Solr Performance Factors(Solr 性能因素)
Riak, haproxy, and client side applications
《CMMI+敏捷整合开发:更快改进性能的案例与实用技术》( Integrating CMMI and Agile Development: Case Studies and Proven Techniques for Faster Performance Improvement )扫描版[PDF]
Supra Skytop Shoes All White Gunny are a top performance
SOLR Performance Benchmarks – Single vs. Multi-core Index Shards
《新火星人-After Effects 5.5 影视合成风暴 3CD》[ISO]
《深入浅出CoffeeScript》(CoffeeScript: Accelerated JavaScript Development)扫描版[PDF]
对Hadoop-HDFS性能造成重大影响的杀手-Shell
CISCO ROUTER 中的隐含命令
升级 Solr 1.4 后性能有所提升
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!