BearPanther.com

Gzip JSON Generated On the Fly

Introduction

user

bear


LATEST POSTS

Using jsformat with Sublime Text 26th July, 2014

There are many d3 demos like this but this one is mine 08th June, 2014

Blog

Gzip JSON Generated On the Fly

Posted on .

We love JSON. It’s simple, human-readable AND browsers love it. My only complaint is that it’s not as efficient as it could be when sending data to a client. Normally, this isn’t an issue. But when you start working with mobile-specific web apps, every byte counts.

Since we’ve been working on something that is mobile specific, sending 100K worth of JSON isn’t ideal. One quick, painless optimization is to have our web server gzip the JSON before sending it. All browsers know how to work with gzipped responses so doing this doesn’t require any extra work on the client.

But what are the specifics of the implementation? Well, it’s just a couple lines in an .htaccess file:


AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/json

As with most web-programming related things, I found this via some vigorous searching. The specific blog post I found was titled “Enabling gzip, deflate compression on your website on apache via .htaccess”. I added the “application/json” portion on the end to get gzipped responses out of apache since my JSON is served with a content-type header of “application/json”. Easy enough, right? Adding that line reduces our JSON from ~100K to ~20K. Pretty awesome for a tweak that took all of ten minutes.

Comments
user

Author Chris Jones

Posted at 4:16 pm February 26, 2013.

I have spent the past couple of days trying to optimize my project’s load time and this golden nugget of information is EXACTLY what I needed. Thank you so much!

Reply
user

Author Mark Nadig

Posted at 6:07 pm May 24, 2013.

Would love to know what type of increase in load you saw on the server. From what I’ve read, gzip is best if payloads are over a certain threshold. The latency in gzipping a 30k response may not be worth it. Just looking for any insight into your experience.

Reply
    user

    Author bear

    Posted at 1:47 am May 27, 2013.

    Hi Mark,

    Honestly, we haven’t monitored what kind of stress this puts on the server. The goal was to get response sizes down so that pages load faster over networks with less bandwidth (read: mobile).

    Reply

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

View Comments (3) ...