Zulfiqar's weblog

Architecture, security & random .Net

JSON vs XML encoder performance

Posted by zamd on December 16, 2008


Tim has summarized that both JSON and XML has their unique place. JSON encoded data has smaller wire footprint and is easier to convert back into Javascript objects using javascript:eval function so it’s generally favoured in AJAX scenarios. I was asked about WCF encoding performance for JSON and XML (text). My initial guess was there shouldn’t be much difference in encoding performance so to validate my assumption I wrote following code to test the encoding performance and indeed it’s roughly the same.

 

To encode same message 300000 times it  took following number of ticks (roughly equal).

 

XML encoding ticks = 63810463

JSON encoding ticks = 68490315

I used following code in my testing.

 

namespace encodingPerformance

{

    [DataContract]

    public class Data

    {

        [DataMember]

        public string Name { get; set; }

        [DataMember]

        public string Address { get; set; }

        [DataMember]

        public double Salary { get; set; }

        [DataMember]

        public string Email { get; set; }

        [DataMember]

        public string SpouseName { get; set; }

        [DataMember]

        public int FamilyCount { get; set; }

        [DataMember]

        public int Age { get; set; }

    }

    class Program

    {

        static void Main(string[] args)

        {

            var d = new Data

            {

                Address = "15 ********* aveneu.",

                Age = 29,

                Email = "uzhadaf@ gamicl.com",

                FamilyCount = 3,

                Name = "******* rAhmeD2",

                Salary = 111212121.121,

                SpouseName = "Sigrasdf"

 

            };

 

            const int ITER = 3000000;

 

            var ms = new MemoryStream();

            var tbe = new TextMessageEncodingBindingElement();

            tbe.MessageVersion = MessageVersion.None;

 

            var msg = Message.CreateMessage(MessageVersion.None, "", d);

            var buf = msg.CreateBufferedCopy(int.MaxValue);

 

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < ITER; i++)

            {

                msg = buf.CreateMessage();

                tbe.CreateMessageEncoderFactory().Encoder.WriteMessage(msg, ms);

                ms.Seek(0, SeekOrigin.Begin);

            }

 

            sw.Stop();

            Console.WriteLine("XML encoding ticks = {0} ", sw.ElapsedTicks);

 

            msg = Message.CreateMessage(MessageVersion.None, "", d, new DataContractJsonSerializer(typeof(Data)));

            buf = msg.CreateBufferedCopy(int.MaxValue);

            var wbe = new WebMessageEncodingBindingElement();

            var prop = new WebBodyFormatMessageProperty(WebContentFormat.Json);

 

            // measure JSON encoding time….

            sw = Stopwatch.StartNew();

            for (int i = 0; i < ITER; i++)

            {

                msg = buf.CreateMessage();

                msg.Properties.Add(WebBodyFormatMessageProperty.Name, prop); //JSON encode

                wbe.CreateMessageEncoderFactory().Encoder.WriteMessage(msg, ms);

                ms.Seek(0, SeekOrigin.Begin);

            }

            sw.Stop();

            Console.WriteLine("JON encoding ticks = {0} ", sw.ElapsedTicks);

 

        }

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: