Bug Tracker

Modify

Ticket #9889 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

Use Web Workers for AJAX requests

Reported by: andrearo@… Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.6.2
Keywords: Cc:
Blocking: Blocked by:

Description

Would it be possible to use HTML5 Web Workers to improve the performance of AJAX requests in jQuery?

It seems that Web Workers have access to the XMLHttpRequest object. Running AJAX requests, waiting for a response, and parsing the resulting JSON, in a separate thread will surely improve performance.

It would be nice if jQuery offered Web Worker-enabled AJAX requests as a feature included in jQuery by default.

Change History

comment:1 Changed 3 years ago by timmywil

  • Priority changed from undecided to low
  • Status changed from new to open
  • Version changed from git to 1.6.2
  • Component changed from unfiled to ajax
  • Milestone changed from None to 1.next

We can table this for discussion in a later release since feature requests for 1.7 are closed, but I'm ok with seeing how much code it would take and how much performance would benefit.

comment:2 Changed 3 years ago by rwaldron

  • Status changed from open to closed
  • Resolution set to wontfix

I've spent a lot of time researching and experimenting with WebWorkers and I can definitively say that this is not a viable or realistic addition to jQuery core.

comment:3 Changed 3 years ago by andrearo@…

rwaldron, could you please elaborate?

comment:4 Changed 3 years ago by rwaldron

This is copied/pasted from -dev channel discussion:

  1. spin up cost of workers is gross
  2. it would be slower for single ajax requests

ie. GlobalScope [ postMessage() ] -> WorkerGlobalScope [ onmessage or addEventListener( message ...) ] -> XHR (request) (response) -> postMessage() -> GlobalScope [ onmessage or addEventListener( message ...) ] -> Ajax Handler on client? vs... XHR (request) (response) (current) if you had a system that was _polling_ eg. open a worker, make xhr requests in a loop forever, on each response, postMessage() the results back to the client

  1. We can't create _real_ workers in IE6, 7, 8 and 9 because a real Worker is actually an OS level thread that's capable of being spun up over multiple cores

eg. user A has a quad core, user B has dual core, they both visit a website that makes use of a Renderer (ie. the window) + 2 Workers, so 3 processes in all

  • For A: Core 1: Renderer; Core 2: Worker; Core 3: Worker
  • For B: Core 1: Renderer; Core 2: Worker; Core 1: Worker

so, thats the argument against those bogus "setTimeout" loops pretending to be Workers

  1. jQuery will throw dom security exceptions when run in WorkerGlobalScope because window and document don't exist and jQuery explicitly refers to them
Version 0, edited 3 years ago by rwaldron (next)

comment:5 Changed 3 years ago by dmethvin

Yeah, $.ajax is already quite asynchronous since the request and network activity happens in parallel with whatever is going on with JavaScript on the page. All that the web worker buys you is some extra parallelism in the JSON parsing and completion functions, but as rwaldron mentions you wouldn't be able to do any DOM operations there because of the security/thread-access restrictions. For that reason alone it's pretty much a non-starter.

You mentioned it would "surely improve performance", do you have any code examples?

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.