Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON parsing on server vs. parsing client side

I'm writing a node app and need to server a bunch of JSON map data. In order for the data to properly show up, it must be parsed.

Now I'm wondering if it's better to parse on the server (and send this parsed data object to the client) or send pure json and have it be parsed on the client side after the ajax call.

My biggest concern with doing it on the server is that many simultaneous queries could slow everything down for a whole bunch of people.

Server side code option:

var data = [];
db.simple_query([{ask: COMPLEX QUERY TO RETURN DATA AS JSON }], function(err, geo_data){
                        if (err) {
                            callback(err);
                        }
                        else{
                            data.push(JSON.parse(geo_data.rows[0].row_to_json));
                            callback();
                        }
                    });

Client side code option:

$.ajax({
                    type: 'GET',
                    url: url,
                    success: function(data){
                        if (data){
                            $.each(data, function(i, geo){
                                L.geoJson(JSON.parse(geo)).addTo(map);
                            });
                        }
                    },
                    error: function(data){
                        //Error Handling
                    }
                });
like image 575
sir_kitty Avatar asked Oct 20 '25 01:10

sir_kitty


2 Answers

It really doesn't matter. Compared to a database access (or basically anything that involves network or disk activity) serializing/deserializing JSON is blazingly fast. Don't over-optimize things that do not need optimization.

However, if you want to pass the data to the client without any server-side processing and already get a JSON string it would be pointless to deserialize it on the server and then serialize it again to transmit it to the client which will again have to deserialize it.

like image 105
ThiefMaster Avatar answered Oct 21 '25 16:10

ThiefMaster


If this is a service that you intend to maintain, scale, or support moving forward then I'd probably go w/ parsing on the server. If not and you are just jamming this out for some short term need or being tactical for whatever reason, then I'd just parse it on the client. Either way, I expect the impact of the JSON parsing / serialization to be negligible... which is why I'd consider other factors when making the decision.

If you go with server side parsing you'll be left in a good position for a number of reasons. First, you'll be able to provide a better contract(s) for this data (e.g. decompose the data into cohesive sets of responsibilities. Hide leaky/ugly details. Reduce and prune the size of the payload to just what makes sense for you). Second, should you choose to use a different data provider or the underlying data provider changes or you wish to change any of the implementation details (e.g. your parsing algorithm) you can shelter these decisions from the client(s). Also, your options on the server for scaling or optimizing the performance of the service are many (e.g. caching, parsing optimization, additional machines, etc) and these implementation details can also be hidden from consumers.

All in all, I'd provide a good set of contracts and do the parsing server side in order to hide my implementation details, unless circumstances (e.g. short timeline, this is a prototype) dictated otherwise.

like image 42
Matt Self Avatar answered Oct 21 '25 15:10

Matt Self



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!