Is JSON.parse delegation to a web worker (in Chrome Extension / FF Addon)?

I am writing a Chrome extension that stores a large amount of data in the localStorage browser and analyzes it on every page load. Now, as data size increases, page load / page performance starts to deteriorate. So I decided to pass the parsing to the web worker. But I doubt it is worthy. What I can do is pass my line, which will be parsed for such an employee.

worker.postMessage(localStorage['myObj']);

And I plan to parse this string in JSON and send it back to the main stream, for example

worker.onMessage(function(myObj){
    //Then Play around with my object here.
});

But since I searched in terms of the performance of this method, including messages and posting messages, as well as the fact that some browsers do not allow JSON objects to be sent to a message and some serialize it automatically when sending, I doubt that this method deserves attention.

Since my application is just a Chrome extension as well as Firefox Addon, I am only interested in these two browsers. Can someone suggest me if this method is suitable for these two browsers?

+3
source share
2 answers

WebWorkers WebWorkers , . JSON WebWorker, , , script. , , JSON , .

2017: , . . ( ) .

+1

. , , .

- , . JSON- ( , , , JSON), JSON.

, , , , JSON, , JSON, , , , .


JSON:

// This stands in for 'worker.js':
var blob = new Blob([
  'this.onmessage = function(message) {\n' +
    'postMessage(JSON.parse(message.data));\n' +
  '};'
  ], { type: "text/javascript" });
var workerUrl = URL.createObjectURL(blob);

// Main script:
var w = new Worker(workerUrl/*"worker.js"*/);
w.onmessage = function(message) {
    display("Got response: typeof message.data: " + typeof message.data);
    display("message.data.for = " + message.data.foo);
};

display('Posting JSON \'{"foo":"bar"}\' to worker');
w.postMessage('{"foo":"bar"}');

function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
}
body {
  font-family: monospace;
}
Hide result

:

Posting JSON '{"foo":"bar"}' to worker
Got response: typeof message.data: object
message.data.for = bar
+7

All Articles