Using AJAX/JSON and performance issues with eval()

Discussion in 'Javascript' started by bizt, Jul 1, 2008.

  1. bizt

    bizt Guest

    Hi,

    Im currently looking to move into using JSON for AJAX instead of
    returning from the server a string like the following:

    12345{This is a text string{true[1234|This is another set of fields|
    null

    This is returned from the server and I use split() and loops to
    generate JS arrays / nested arrays at the client end. I cannot use XML
    is it is deemed to heavy for the website as we deal with very high
    volumes of traffic and trying to reduce the data sent back and forth
    through the client and servers. The problem with the above is that it
    makes my life as a JS programmer harder coz I have to do all this
    splitting and looping to seperate the data

    Now JSON seems the perfect on both fronts - it is lightweight and I
    only have to use a single eval() to replace what splits() and loops
    done previously. I can also have as many levels and nesting as I wish.
    There are a couple of conserns I have here tho:

    Eval() I have heard can be quite slow? Is this a serious concern for
    large data sets? Even if it is slow, is it still going to be faster
    than my previous method above?

    I understand that you can have a JSON format for JS arrays and JS
    objects:

    // array
    {0: 12345,2:"This is a text string"}

    // object
    [12345,"This is a text string"]

    I prefer objects because I dont need to specifiy the index key and I
    can treat it exactly like an array - oSet[0] = ...
    Are objects just as quick to access/alter as arrays? I have a few
    simultaneous client processes going on (ie. refreshing multiple HTML
    tables automatically in the background) and really want it to perform
    well.


    Is there anything else worth considering when using JSON? My website
    is mainly just updating content on multiple tables and refreshing data
    every so often, thats about the extent of it.

    Thanks

    Burnsy
     
    bizt, Jul 1, 2008
    #1
    1. Advertising

  2. bizt wrote:
    > Im currently looking to move into using JSON for AJAX instead of
    > returning from the server a string like the following:
    >
    > 12345{This is a text string{true[1234|This is another set of fields| null
    >
    > [...] The problem with the above is that it makes my life as a JS
    > programmer harder coz I have to do all this splitting and looping to
    > seperate the data
    >
    > Now JSON seems the perfect on both fronts - it is lightweight and I only
    > have to use a single eval() to replace what splits() and loops done
    > previously. I can also have as many levels and nesting as I wish. There
    > are a couple of conserns I have here tho:
    >
    > Eval() I have heard can be quite slow? Is this a serious concern for
    > large data sets?


    I don't think so.

    > Even if it is slow, is it still going to be faster than my previous
    > method above?


    Probably yes. eval() evaluates its string argument as an ECMAScript
    Program. This evaluation is implemented in native, already compiled (with
    few exceptions platform-dependent) code. That should be considerably faster
    to execute than your implementation, which needs to be JIT-compiled and the
    resulting byte-code interpreted by a VM first.

    > I understand that you can have a JSON format for JS arrays and JS
    > objects:


    Just to add to confusion: JS arrays are implemented as objects, Array
    objects. What you call "JS objects" here, are (augmented) Object objects.

    > // array
    > {0: 12345,2:"This is a text string"}
    >
    > // object
    > [12345,"This is a text string"]
    >
    > I prefer objects because I dont need to specifiy the index key and I can
    > treat it exactly like an array - oSet[0] = ...


    The above is the reason for that. `0' is the name of either object's
    property, only that the method of property access with Array objects differs
    slightly from that of other objects.

    > Are objects just as quick to access/alter as arrays?


    I think they are quicker to alter than arrays when it comes to inserting
    items, and just as quick when accessing items. To be sure, you could do an
    estimation of general runtime efficiency on the corresponding algorithms in
    the ECMAScript Specification, Edition 3 Final, and compare with empirical
    results for different input.

    > I have a few simultaneous client processes going on (ie. refreshing
    > multiple HTML tables automatically in the background) and really want it
    > to perform well.
    >
    > Is there anything else worth considering when using JSON? My website is
    > mainly just updating content on multiple tables and refreshing data every
    > so often, thats about the extent of it.


    When dealing with tables, you should consider a combination of both:

    [
    {x: 42, z: 23},
    {x: 1337, z: 1701}
    ]


    PointedEars

    P.S.
    Please don't use off-Usenetter slang like "coz" here. Besides presenting
    you as a semi-literate, this is an international newsgroup.
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Jul 1, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    265
    Florian Frank
    Jun 30, 2009
  2. Replies:
    2
    Views:
    109
    Matt Kruse
    Jun 9, 2006
  3. sajuptpm
    Replies:
    2
    Views:
    405
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    311
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    295
    Bryan Britten
    May 28, 2013
Loading...

Share This Page