JSON.parse slow as hell?

1,381 views
Skip to first unread message

Christoph Husse

unread,
Oct 18, 2012, 10:15:02 AM10/18/12
to mi...@dartlang.org
Hi,

I am using JSON.parse to load meshes and textures from a webserver. I thought that JSON.parse() would actually invoke a browser native code variant? Thus I expected it to be fast.

My Benchmark looks like this:

[INFO]: Loading! // starting to query resource from local server, a (too detailed) BlackHawk helicopter
[INFO]: [0 ms]: Parsing JSON... // here only JSON.parse() is called on a 2 megabyte JSON string
[INFO]: [1443 ms]: Importing JSON... // creating useful objects and assigning the parts of the parsed JSON data... This is blazingly fast to my suprise!
[INFO]: [1450 ms]: Done! // Yeah, mesh loaded!
[INFO]: Success!

This runs on Dartium, in the VM, without debug/checked-mode. So this should be the fastest setting. 

Is there a chance that this runs MUCH faster in a real browser (I am talking about least one order of magnitudes)? Because otherwise this would suck. Currently I can't test it, since Dart2Js is broken on my sources, since it obviously can't handle static functions as default parameter value...

This JSON string is parsed in NO time, by a C++ JSON parser, and I would expect browser to be similarily fast, at least not 1000 times slower...

Vijay Menon

unread,
Oct 18, 2012, 10:24:57 AM10/18/12
to mi...@dartlang.org
It is pure Dart code at the moment and definitely slower than the native implementation.  There is an open bug to use the browser's native implementation:


Cheers,

Vijay

--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

Christoph Husse

unread,
Oct 18, 2012, 10:32:53 AM10/18/12
to mi...@dartlang.org
Okay... I was a bit stupid first ^^. I had a lot of baggage around this server request but it was actually much easier to start from scratch in a new project than I thought. So I got it working in "Chromium" at least, because there I can turn off same-origin-policy, which is needed to load the local JSON file.

There I get 164 milliseconds. So my initial bet of one order of magnitudes came true. I am not sure how much improvement we would see in the latest Chrome/Firefox, but I doubt it will be significant?
Well, I can live with that, especially since Dart can do this in a separate Isolate, since all parsed data adheres to the message passing restrictions.

Christoph Husse

unread,
Oct 18, 2012, 10:37:23 AM10/18/12
to mi...@dartlang.org
>It is pure Dart code at the moment and definitely slower than the native implementation.  There is an open bug to use the browser's native implementation:

Uh that one bug you posted there is weird. Since I was talking about the DartVM... Which is obviously ten times slower with JSON.parse than Dart2Js... But that bug says that Dart2Js is using the unoptimized version and should use the browser's native one? Now I am confused. So can it be even faster, when this bug is fixed? And how much ^^?

Vijay Menon

unread,
Oct 18, 2012, 11:04:45 AM10/18/12
to mi...@dartlang.org
On Thu, Oct 18, 2012 at 7:37 AM, Christoph Husse <thesai...@googlemail.com> wrote:
>It is pure Dart code at the moment and definitely slower than the native implementation.  There is an open bug to use the browser's native implementation:

Uh that one bug you posted there is weird. Since I was talking about the DartVM... Which is obviously ten times slower with JSON.parse than Dart2Js... But that bug says that Dart2Js is using the unoptimized version and should use the browser's native one? Now I am confused. So can it be even faster, when this bug is fixed? And how much ^^?


In Chrome/V8, JSON.parse calls into native C++ code.  I believe at the moment, neither Dart2JS or Dartium/DartVM is doing that.  Once it's tuned, it should be competitive with the V8 / native implementation on both.  I hadn't seen any numbers around it recently until yours, and I'm a little surprised the VM is ~10x slower (if I understand your numbers correctly).

Here's the actual implementation:

Christoph Husse

unread,
Oct 18, 2012, 11:06:03 AM10/18/12
to mi...@dartlang.org
> hadn't seen any numbers around it recently until yours, and I'm a little
> surprised the VM is ~10x slower (if I understand your numbers correctly).
>

Yes me too... Would you say I should file a bug?

Vijay Menon

unread,
Oct 18, 2012, 11:15:57 AM10/18/12
to mi...@dartlang.org
Yes, please do!

Christoph Husse

unread,
Oct 18, 2012, 11:34:53 AM10/18/12
to mi...@dartlang.org

Christoph Husse

unread,
Oct 18, 2012, 1:28:53 PM10/18/12
to mi...@dartlang.org
For the sake of completeness, I have modified the Dart2Js output, by replacing line 2744

    $.indexSet(response, 'response', $.JSON_parse($.index(request, 'json')));

with
    $.indexSet(response, 'response', JSON.parse($.index(request, 'json')));

Of course, this does not really work. Only the timing works, but JSON.parse is actually incompatible to Dart, so it seems ;).
...

Now I get

> Parsing JSON at "BlackHawk.json" took 34 ms.
> Parsing JSON at "BlackHawk.json" took 34 ms.
> Parsing JSON at "BlackHawk.json" took 34 ms.

in Chromium. So by using native JSON.parse in Dart2Js, this would yield another 5x speedup... Good to know :). 34ms is pretty good actually, this would allow quite some models per second to be processed.

Christoph Husse

unread,
Oct 20, 2012, 3:21:25 AM10/20/12
to mi...@dartlang.org
Okay I got my JSON parser down to at least Chrome speed ;). I can probably also squeeze out even more, since there are still hotspots, and then one has to note that this is currently running in a VM, so I think the real numbers will be better.
I will probably try to integrate this parser into DartVM today ^^. We will see how much performance is lost thanks to creating dart objects... But the raw parsing speed should now be on par with current browsers.
Reply all
Reply to author
Forward
0 new messages