How to create the V8 object in another thread, then copy it back into nodejs scope?

StackOverflow https://stackoverflow.com/questions/21751439

  •  11-10-2022
  •  | 
  •  

문제

I have to parse very long JSON text into JSON object. I tried to measure the executing time with following code.

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

It takes about 0.5 millisecond, that is quite a lot. because the nodejs is single-threaded for its V8 Engine, if this operation is so heavy & frequent, the throughput will be impacted much.

Hence I plan to write an asynchronous JSON.parse in C as a NodeJS native addon, using uv_queue_work to let the heavy operation happens in another thread, taking use the multi-core and avoid taking up the nodejs main loop.

The problem is, the V8 object in nodeJS is not allowed to be accessed from other thread than main thread.

Is there any way that I can parse the text and create the V8 object in another thread, then copy the new created V8 object into the main thread ?

Br

도움이 되었습니까?

해결책

Is there any way that I can parse the text and create the V8 object in another thread, then copy the new created V8 object into the main thread ?

There is no good way to do that.

If you want to use all cores, it's better to spawn multiple node.js processes using cluster module and let each of them handle different requests.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top