网络科技

    今日:347| 主题:244604
收藏本版
互联网、科技极客的综合动态。

[其他] uws: The importance of JavaScript scope control

[复制链接]
ぁ詠遠∝僾妳 发表于 5 天前
62 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
With JavaScript in the picture, you always need to be careful with how you express yourself. Since everything is dynamic and implicit rather than static and explicit, you often find yourself writing far less efficient code than what could be done. Things that seem identical often have major scalability differences when run at scale.
  Memory usage

  So far we have been able to get 1 million WebSockets in Node.js using 1.2 GB of user space memory. In comparison, this is ~12x more scalable than    ws, the best WebSocket alternative out there for Node.js. However, I recently realized that we can actually reach 1 million connections using only 781 MB from Node.js - a whopping ~20x advantage over    ws.  
  I've done some minor changes to the JavaScript code, but the most important factor is how the user code looks like. Since µWS is majorly bottlenecked by JavaScript, even the smallest change to your JavaScript code will result in a great deal of improvement:
  1. wss.on('connection', (ws) => {
  2.     ws.on('message', (message) => {
  3.         ws.send(message);
  4.     });
  5. });
复制代码
The above code is terrible for scalability since it defines the (anonymous) function inside of the connection handler. As any JavaScripter should know, this will propagate the scope of the current context into the newly defined function. When we then attach this function to the (persistent) WebSocket, we also attach the scope, which thus also becomes persistent.
  Going from ~12x to ~20x scalability advantage only requires the user to swap their code with this or similar:
  1. function onMessage(message) {
  2.     ws.send(message);
  3. }
  4. wss.on('connection', (ws) => {
  5.     ws.on('message', onMessage);
  6. });
复制代码
I tested this change using    wsto see how much of an improvement it made there, but since    wshas far deeper scalability problems this change didn't improve anything significantly or even remotely measurable.
友荐云推荐




上一篇:Control Anything with a Universal Wireless Remote
下一篇:Mail-in-a-Box – one-click, easy-to-deploy email server
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

怜容 发表于 5 天前
看完帖子,洗洗睡了!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表