Web Workers handling AJAX calls - optimisation overkill?

Konrad Dzwinel

I'm working with a code that handles all AJAX requests using Web Workers (when available). These workers do almost nothing more than XMLHttpRequest object handling (no extra computations). All requests created by workers are asynchronous (request.open("get",url,true)).

Recently, I got couple of issues regarding this code and I started to wonder if I should spend time fixing this or just dump the whole solution.

My research so far suggests that this code may be actually hurting performance. However, I wasn't able to find any credible source supporting this. My only two findings are:

  • 2 year old jQuery feature suggestion to use web workers for AJAX calls
  • this SO question that seems to ask about something a bit different (using synchronous requests in web workers vs AJAX calls)

Can someone point me to a reliable source discussing this issue? Or, are there any benchmarks that may dispel my doubts?

[EDIT] This question gets a little bit more interesting when WebWorker is also responsible for parsing the result (JSON.parse). Is asynchronous parsing improving performance?

Konrad Dzwinel

I have created a proper benchmark for that on jsperf. Depending on the browser, WebWorker approach is 85-95% slower than a raw ajax call.


Notes:

  • since network response time can be different for each request, I'm testing only new XMLHttpRequest() and JSON.parse(jsonString);. There are no real AJAX calls being made.
  • WebWorker setup and teardown operations are not being measured
  • note that I'm testing a single request, results for webworker approach may be better for multiple simultaneous requests
  • Calvin Metcalf explained to me that comparing sync and async on jsperf won't give accurate results and he created another benchmark that eliminates async overhead. Results still show that WebWorker approach is significantly slower.
  • From the Reddit discussion I learned that data passed between the main page and WebWorker are copied and have to be serialized in the process. Therefore, using WebWorker for parsing only doesn't make much sense, data will have to be serialized and deserialized anyway before you can use them on the main page.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RequireJS Singleton与Web Workers

来自分类Dev

按URL划分Heroku Web Workers

来自分类Dev

关于Web Workers并行性

来自分类Dev

Web Workers的Javascript功能检测模块支持

来自分类Dev

如何使用Web Workers查找交集?

来自分类Dev

Web Workers文件句柄可以包含或包含模块?

来自分类Dev

Web Workers:SetInterval和SetTimeout可靠性

来自分类Dev

在JavaScript中的Web Workers中使用Synaptic库

来自分类Dev

如何使用webpack处理Web Workers的“标准”语法?

来自分类Dev

是否可以访问.vue组件内的Web Workers

来自分类Dev

使用Firefox扩展程序中的Web Workers的XPCOM

来自分类Dev

在JavaScript中的Web Workers中使用Synaptic库

来自分类Dev

AngularJS: Using $q to fire ajax calls synchronously

来自分类Dev

Yesod: Using typesafe URLs in AJAX calls

来自分类Dev

Jquery - Multiple function calls to one AJAX call

来自分类Dev

Web方法的Ajax请求

来自分类Dev

Ajax调用Web服务

来自分类Dev

从AJAX调用Web服务

来自分类Dev

Ajax调用Web API

来自分类Dev

HTML5 Web Workers可以/应该将CORS用于跨域吗?

来自分类Dev

在独立的V8 Javascript引擎中使用Web-Workers

来自分类Dev

javascript web workers 多线程字符串搜索比单线程慢?

来自分类Dev

密码算法hash_algos overkill

来自分类Dev

密码算法hash_algos overkill

来自分类Dev

Handle Rails flash messages after AJAX calls using ReactJS

来自分类Dev

Ajax调用Web API方法

来自分类Dev

Ajax Web服务请求

来自分类Dev

Identify item type when handling event in Exchange Web Services (EWS)

来自分类Dev

Docker-使用Redis,Postgres,ElasticSearch,NGINX,Workers和多个ruby应用程序设置Web应用程序的建议