admin管理员组

文章数量:1023738

In my react-native app I need to stringify (serialize) big objects and not to block js thread - asynchronous api that uses another threads, something like this:

JSON.stringifyAsync({ foo: "bar" }).then(x => console.log(x));

Please don't suggest to wrap JSON.stringify into Promise, it just defers blocking of js thread.

In my react-native app I need to stringify (serialize) big objects and not to block js thread - asynchronous api that uses another threads, something like this:

JSON.stringifyAsync({ foo: "bar" }).then(x => console.log(x));

Please don't suggest to wrap JSON.stringify into Promise, it just defers blocking of js thread.

Share Improve this question edited Jun 15, 2022 at 10:46 gentlee asked Nov 1, 2016 at 20:49 gentleegentlee 3,7251 gold badge33 silver badges47 bronze badges 6
  • 4 So the first choice didn't work. What streaming JSON parser have you tested and what problems did you find there? Do you have any information on what you've already done? – ssube Commented Nov 1, 2016 at 20:54
  • I don't need streaming json parser, i need just true async one. If you have an answer pls just give it. And whats the reason of your downvote? – gentlee Commented Nov 1, 2016 at 21:01
  • 1 @ssube - How would a streaming parser help with OP's problem? – Ted Hopp Commented Nov 1, 2016 at 21:19
  • I'm pretty sure that streaming parser would help, because by its nature it's async, hence non-blocking, for example this one should do the job: npmjs./package/json-stream-stringify – Maxim Mazurok Commented Jun 10, 2022 at 2:36
  • @MaximMazurok if it is 100% JS then it can't be non-blocking. – gentlee Commented Jun 14, 2022 at 10:39
 |  Show 1 more ment

2 Answers 2

Reset to default 7

I don't think this is currently possible. It's an issue for node.js (see here, here, and here) and I don't think the situation is any better for browsers or other JS engines.

There is an EcmaScript proposal (see here) to add functions JSON.parseAsync and JSON.stringifyAsync methods. I don't know the status of this proposal. From the proposal: "There is no way of transpilating the actual effect of this proposal." So it looks like the proposer(s) agree that there's currently no way to do this.

Maybe you can split the objects and merge the processed parts at the end.

Or do something like this:

async.eachOf(obj,function(value,key,callback){
  async.setImmediate(function(){

    // do something

  });
}, function(err,reply){

});

In my react-native app I need to stringify (serialize) big objects and not to block js thread - asynchronous api that uses another threads, something like this:

JSON.stringifyAsync({ foo: "bar" }).then(x => console.log(x));

Please don't suggest to wrap JSON.stringify into Promise, it just defers blocking of js thread.

In my react-native app I need to stringify (serialize) big objects and not to block js thread - asynchronous api that uses another threads, something like this:

JSON.stringifyAsync({ foo: "bar" }).then(x => console.log(x));

Please don't suggest to wrap JSON.stringify into Promise, it just defers blocking of js thread.

Share Improve this question edited Jun 15, 2022 at 10:46 gentlee asked Nov 1, 2016 at 20:49 gentleegentlee 3,7251 gold badge33 silver badges47 bronze badges 6
  • 4 So the first choice didn't work. What streaming JSON parser have you tested and what problems did you find there? Do you have any information on what you've already done? – ssube Commented Nov 1, 2016 at 20:54
  • I don't need streaming json parser, i need just true async one. If you have an answer pls just give it. And whats the reason of your downvote? – gentlee Commented Nov 1, 2016 at 21:01
  • 1 @ssube - How would a streaming parser help with OP's problem? – Ted Hopp Commented Nov 1, 2016 at 21:19
  • I'm pretty sure that streaming parser would help, because by its nature it's async, hence non-blocking, for example this one should do the job: npmjs./package/json-stream-stringify – Maxim Mazurok Commented Jun 10, 2022 at 2:36
  • @MaximMazurok if it is 100% JS then it can't be non-blocking. – gentlee Commented Jun 14, 2022 at 10:39
 |  Show 1 more ment

2 Answers 2

Reset to default 7

I don't think this is currently possible. It's an issue for node.js (see here, here, and here) and I don't think the situation is any better for browsers or other JS engines.

There is an EcmaScript proposal (see here) to add functions JSON.parseAsync and JSON.stringifyAsync methods. I don't know the status of this proposal. From the proposal: "There is no way of transpilating the actual effect of this proposal." So it looks like the proposer(s) agree that there's currently no way to do this.

Maybe you can split the objects and merge the processed parts at the end.

Or do something like this:

async.eachOf(obj,function(value,key,callback){
  async.setImmediate(function(){

    // do something

  });
}, function(err,reply){

});

本文标签: Parallel stringify (serialize) to JSON in javascriptStack Overflow