How to create a V8 object in another thread and then copy it back to the nodejs area?

I have to parse a very long JSON text into a JSON object. I tried to measure the runtime using the following code.

var t = process.hrtime()
JSON.parse(jsonStr);
t = process.hrtime(t);

It takes about 0.5 milliseconds, which is quite a lot. because nodejs is single-threaded for its V8 Engine, if this operation is so heavy and frequent, performance will be greatly affected.

Therefore, I plan to write asynchronous JSON.parse in C as a native NodeJS addon, using uv_queue_workso that a heavy operation occurs on another thread using a multi-core processor and avoiding using the main nodejs loop.

The problem is that the V8 object in the JS host cannot be accessed from a thread other than the main thread.

Is there a way that I can parse text and create a V8 object in another thread, and then copy the newly created V8 object to the main stream?

Vg

+3
source share
1 answer

Is there a way that I can parse text and create a V8 object in another thread, and then copy the newly created V8 object to the main stream?

There is no good way to do this.

If you want to use all the kernels, it is better to create several node.js processes using a module clusterand let each of them handle different requests.

+1
source

All Articles