开发者

Custom JSON.stringify fails to Stringify object as whole, but works when iterated one level deep

Hoping someone can spot the error, because I'm having trouble

Alright, I built my own JSON.stringify for just custom large objects. It may not be exactly to specification for some edge case things, but's only meant for stringify on large objects that I'm building myself.

Well, it works, and works well for most objects, but I have an Object I'm trying to stringify and it's failing and printing this before exiting:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
undefined

Not very helpful. The object is fine because the regular call to JSON.stringify(object) works fine, and when I iterate over the object with for (var x in obj) if (obj.hasOwnProperty(x)) { myStringify(obj); } that works fine, but if I call it on the top level of the object, it goes to hell... It doesn't really make sense to me, and the only thing I can think of is the level if recursion is somehow breaking something...

The Parser : https://gist.github.com/958776 - The stringify function I'm calling

ObjectIterator.js : https://gist.github.com/958777 - Mostly to provide the asynchronous iteration

Edit So, I iterated over the object one level deep and compared the resulting string to the string of JSON.stringify(sameLevelDeep) and they're equal. Since the output is equal, I'm not sure that 开发者_JS百科it's how I'm parsing something, but possible that it's such a large object or the amount of recursion is so high?

Edit 2 So, I "fixed" the problem, I guess. Instead of every 25th iteration being pushed to the next event loop, I push every fifth. I'm not sure why this would make a difference but it does... I guess the question is now "Why does that make a difference"?


Okay well, beyond it being a very specific question helping a very specific person, I would like to take this to a different place, that might also remove your problem and maybe help others.

Since you are not specifying why you are going through this process, I will have to break it down and guess -- and provide a solution for each guessed idea.

1. (Browser) You are trying to use JavaScript to crunch data, and provide the user with a result

Downloading at least several megabytes of raw data ("some of these objects are 5-10million characters") on a webpage to process and display a result is far from optimal, you should probably be doing this operation on the server side and download the pre-calculated result.

Besides, no matter what you are doing, JavaScript does not support threads. setTimeout(1, function() { JSON.stringify(data); }); shouldn't be much different from what you are doing.

2. (Browser) You are trying to display the downloaded content

You should attempt downloading smaller chunks instead of the whole 10+ million character content using the built-in JSON.stringify method.

3. (Non-browser) You are trying to use JavaScript for an application that requires threading

You should consider using a different programming language for this application.

In summary

I think you are climbing the wrong mountain, you can achieve the same thing walking around it without breaking sweat. If you want to climb a mountain for kicks, there are mountains out there that need it -- but it's not this one.

Translation: Work on the architecture to obsolete the obstacle instead of trying to solve it, if you want to solve a problem there are problems that need a solving -- but it's not this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜