Crafting the SAPO Campus platform, a thousand pixels at a time.
Search
Latest posts

High Performance Javascript - dealing with JSON

No Widget is an Island

Designing Email - Part II

Designing Email - Part I

The amazing "widget" object

The Facebook Witch Project

SAPO Campus keynote from OSL Unesco demonstration session

SAPO Campus session at the UNESCO OSL Seminar

SAPO Campus - Eden Seventh Open Classroom Conference

SAPO Campus leaflet for the UA freshmen

Archive

Janeiro 2011

Novembro 2010

Fevereiro 2010

Dezembro 2009

Novembro 2009

Outubro 2009

Setembro 2009

Julho 2009

Maio 2009

Abril 2009

Março 2009

Fevereiro 2009

Dezembro 2008

Novembro 2008

Outubro 2008

Setembro 2008

Quinta-feira, 6 de Janeiro de 2011
High Performance Javascript - dealing with JSON

We love JSON. It's a highly interchangeable, lightweight data format. JSON is particularly better suited at passing data around than say XML or HTML because of its low ratio of  structure to data. Besides the fact that it's so light over the wire, there are a huge number of libraries for encoding and decoding JSON on the server-side. On the client-side of things, JSON is trivial to parse, allowing you to spend less time parsing data and more time actually doing something with it.

With all of these advantages under its belt, JSON was a natural choice when we at SAPO Campus wondered how we'd pass data around between servers and Javascript-powered applications. When using JSON, two of the most common operations revolve around either parsing strings into valid Javascript Literal Objects or converting the latter into strings. At the first stages of development of the My UA platform, I found a small Javascript library called JSONUtils that handled these two tasks. You might be asking: but don't browsers do JSON parsing and stringifying natively? At the time, only a few did, so we ended up not caring about it and just using JSONUtils every time.

Going native

Yesterday, while auditing some of My UA's code for performance, the thought of native JSON handling popped up in my head. Native implementations are always better than using another script, and these two operations are so commonplace that even a gain of just a few milliseconds could have a major impact on performance. After making the change to use native JSON when available, I started wondering about the real impact on performance the switch to the native implementation had. So I wrote a little test case.

The test

I coded up a simple page that parsed a sample JSON string and stringified a similar JS literal object for, oh say, 20000 iterations, then measured the time it took for the 20000 parsing and stringifying operations to run. I did this for both operations using JSONUtils and native JSON. The results are below:

The results

JSON test case - Parsing

 JSON test case - Stringifying

A few notes on the experiment:

A few thoughts

Native JSON is almost always much, much faster than the JSONUtils implementation. Google Chrome presents the strangest results, with native parsing actually being slower than its JSONUtils counterpart. Native parsing in Google Chrome is actually the slowest of all browsers tested. This is probably a bug in the otherwise excellent V8 engine, but it's still an annoying and potentially crippling oversight.

As expected, Internet Explorer provided the biggest gain in performance. Even if gains in other browsers are substantial (Firefox was a bit of a surprise, I admit) the dramatic improvement in IE justifies the change and this whole experiment.

Files used

Here are the files used in the test case.


tags: , , , ,

Published by bruno-abrantes às 16:01

4

De nsimaria a 6 de Janeiro de 2011 às 17:36
Great post! You should checkout MongoDB, a JSON based database engine. Very flexible. Forget about encoding/decoding JSON on the server side.


De nsimaria a 6 de Janeiro de 2011 às 17:36
Forgot the link: http://www.mongodb.org/


De carlossantos a 6 de Janeiro de 2011 às 22:23
Grande post! E admito que fico sempre espantado ao ler os teus posts em inglês... que são fabulosos.

Agora é ver o my.ua a voar no IE :)


De tim a 7 de Janeiro de 2011 às 13:05
Mto bem! :)


Post a comment

About us
Janeiro 2011
Dom
Seg
Ter
Qua
Qui
Sex
Sab

1

2
3
4
5
6
7
8

9
10
11
12
13
14
15

16
17
18
19
20
21
22

23
24
25
26
27
28
29

30
31


tags

todas as tags

subscrever feeds

RSSPosts

RSSComentários

RSSComentários do post