Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top