Monday, February 16th, 2009
Native JSON in Firefox 3.1; Joins IE 8
>The native JSON API is part of the upcoming 3.1 revision of ECMAScript, so we should see it adopted in browsers pretty quickly. It’s also API compatible with json2.js, as you note, so many many web users will get the performance win without apps needing to update.
I suspect that the performance advantage for native JSON is even more pronounced on the encoding side, but I don’t have tests to hand to back it up.
This was a comment by Mike Shaver, VP of Engineering at Mozilla (disclaimer: where I work).
The comment related to the post on the native JSON support in Firefox 3.1:
In case you haven’t heard, one of Firefox 3.1’s awesome new features will be native JSON support. This is totally sweet for two reasons:
- eval’ing JSON in the browser is unsafe. Using native JSON parsing protects you against possible code execution.
- Safely eval’ing JSON with a 3rd party library can be orders of magnitude slower. Native JSON parsing is much faster.
How does native JSON work compared to plain old eval? Simple:
var jsonString = '{"name":"Ryan", "address":"Mountain View, CA"}'; var person = JSON.parse(jsonString); // 'person' is now a JavaScript object with 2 properties; name and addressPretty easy huh? And here’s how to get a JSON string from an object:
var personString = JSON.stringify(person); // 'personString' now holds the string '{"name":"Ryan", "address":"Mountain View, CA"}'“But wait!”, you say. “How is it safer? How much faster is it compared to eval?”. Ok, I’ll show you.
Gotta love it when browsers implement something that just starts to make apps faster without potentially knowing about it.
Related Content:











Thats cool news, but you can always use http://www.json.org/json_parse.js without eval and working in all browsers (works really good).
THe native JSON implementation in FF 3.1 currently fails to recognize the second parameter to parse. Sites that use this thus break in FF 3.1. I filed a bug and gladly it looks like it will be fixed https://bugzilla.mozilla.org/show_bug.cgi?id=476374
Cool.
JS libraries like jQuery will have to update to take advantage of this, right?
This is great, however, since users will still be on old browsers we will still need to worry about them :( That is probably the most frustrating thing about web development.
stringify? are you kidding me? Next we’ll hear: “{‘l’:'o’}”.objectify()
.
right?
.
**sigh**
.
It’s like they try to be different from other languages. Never heard of decode or encode?
@ibolmo This is the quasi standard API from http://json.org/json2.js
The file above will use the native implementation if it is present, by the way.
@Malde, I don’t need a .js. I want someone to explain why they would think stringify is a good method name.
.
/me cries
@Nosredna you better believe jQuery will have a check for native json support and use it if it’s available.