Thu Sep 13 2018
How does JSON help you to improve website performance?
Performance of a website is an essential part to the successful business on the web, By providing content ready to interactive with minimal of waiting time and load on the server to serve the largest number of request. And website speed is one of the factors that is used in the ranking algorithm. JSON has become the de facto standard exchange format on the web today. JSON is quite simple and is akin to a simplified form of JavaScript. Thankfully, we have many fast libraries to parse and manipulate JSON.
JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. It's an open-standard file format that uses human-readable text to transmit data objects consisting of attribute-value pairs and array data types (or any other serializable value). It's a very common data format used for asynchronous browser-server communication. It was derived from JavaScript, but as of 2018, many programming languages include code to generate and parse JSON-format data. The official Internet media type for JSON is application/json. JSON file names use the extension .json.
JSON is easy to work with and has become the standard data format for virtually everything.
Let see how JSON helps to improve a website performance -
Multiple JSON libraries
In ASP.NET the most popular JSON library is Json.NET (Newtonsoft). But ServiceStack, FastJsonParser (System.Text.Json) and even the built-in DataContract JavascriptSerializer may be faster or provide specific features you may need depending on the scenario. If you do a lot of parsing and really care about performance, FastJson Parser is a lot faster than anything else.
Jil
Implemented faster JSON serializer called Jil.
Compress JSON
JSON is just simple text, you can expect to get up to 90% compression. So use gzip wherever possible when communicating with your web services.
Use streams
Most JSON parsing libraries can read straight from a stream instead of a string. This is a little more efficient and preferred where possible.
Don't parsing JSON if not need
For web apps that receive JSON and simply write it to a queue, database, or other storage, try not to ever parse the JSON if you can. When using something like ASP.NET Web API, don’t define your methods expecting specific classes as incoming data and instead read the post body so ASP.NET never parses the JSON. And for services use header values for authentication, so in some scenarios, you never even need to analyze the body of the message. You can just queue it and let your background services do further validation of the data later.
Avoid serializing all fields, null or default values
Check your JSON library settings to see how you can ignore specific fields, omit null values, etc. Most .NET libraries will use DataContract/DataMember attributes and settings.
Use predefined classes
If at all possible, make sure you have a class that matches the JSON structure you are working with. Parsing generic JSON to a JSON.net JObject or generic dictionaries with FastJson is slower than reading that data into a defined class type. This is likely because a lot more metadata is tracked with the generic Json.NET JObject, JArray, JValue objects.
Use shorter field names
Most libraries enable you to specify attributes on your classes to override the names of the fields as they are being serialized. This can allow you to make your field names much smaller when serialized but more human readable in your code.
Serialize/Deserialize JSON objects
In some use cases, you may receive a large object array that you have to break up into smaller pieces. It makes a pretty significant difference in server CPU usage.
Customize the Web API’s
By default Web API uses Json.NET. If you want to use a different one you can override it by making your own MediaTypeFormatter. In some scenarios, you may also want to configure various special settings.
Manual serialization or parsing
Some libraries, like Json.Net and ServiceStack, have the ability to let you tailor the serialization and parsing as it occurs. They basically work like a tokenizer and read or write through the JSON one segment at a time. Depending on your use case it could be slower or faster to do this.
Isolated Testing
For basic testing, you can use Visual Studio’s built-in performance analyzer with a simple console app. Grab a good sample of your JSON and do various serialize/deserialize tests tens of thousands of times in a loop and watch how long it takes, CPU, and memory usage.
Thanks for reading our article. You can share your comments with us in the comment section. Thank you!