tags:

views:

65

answers:

4

I have a pretty big chat app I'm working on, and had a question about best practices for JS data storage...

I have a table populated with AJAXed data from the server, and the Javascript gets some info from that, and also from an internal object, also populated with AJAXed data. I was starting to store information about each chat in an object, like user status, name, etc., and realized... I have this table right here that I can use as data storage. It's persistent, it stays there at least as long as they're in the chat, so why not just add some hidden TDs or spans to it to hold this data, rather than dealing with a Javascript object? I don't know if there'd be a noticeable change in speed (dealing with an object vs having to parse ID tags and strings), but I was just wondering if there was any fundamental reason why using the table for data storage was A Bad Thing.

And, on the flip side, should I cease looking up data using the table altogether, and instead store all the data in an object (along with displaying it in the table)? Or is my current hybrid system (looking up in table for things that are in the table; using an object for things that aren't, rather than using hidden spans/tds) pretty good?

+3  A: 

I was just wondering if there was any fundamental reason why using the table for data storage was A Bad Thing.

I'd say it's fundamentally "bad" for the same reasons we strive not to mingle javascript or css in with the markup. The page HTML should be used for presentation purposes only. I don't think there's going to be a huge difference performance wise whether you use the table or a javascript object, but the javascript object would be much cleaner, IMO.

Chris
I agree; you should store the data in a form as close to the data that's stored on the server as possible and then "rinse" it to display it to the user. The HTML escaping/unescaping you'll end up doing if you make the HTML your canonical data storage will make your code more complex and will eventually bite you in the butt.
gavinandresen
It is a HUGE lost in perfomance.Just have tested simple thing (only on FirefFox 3.6).setting some div's textContent (innerText for Firefox), innerHTML and a property of some object. There will be amount of updates per second (x1000)object: 2200object with 1000 additional properties: 2000div {display:none} .innerText: 70div .innerText: 20div {display:none} .innerHTML: 10div .innerHTML: 10
I really don't know what you're going on about, kirilloid. He said he's working on a chat app. Even if this chat app has 500+ simultaneous users, he is likely to have at most ~50 updates per second. Your numbers seem completely arbitrary and not at all useful or relevant to the question.
Chris
Keep in mind we're talking updates per browser session. While he may be serving thousands of users, any individual user is only going to receive updates for the particular chat sessions he/she is engaged in.
Chris
+2  A: 

Populating the table, whether using innerHTML or DOM methods, is going to be worse performance-wise. Retrieving the data (particularly if via iteration) also becomes less verbose, easier to maintain and, again, better performance-wise. Using an HTML table is also a more fragile option, as if something goes wrong, other markup on the page could become invalid or, worse, it might end up displayed to the user.

There's also the simple fact it just seems hacky, workaroundy and dirty. It's an HTML table that is to present tabular data, it's not an SQL table for data storage.

JavaScript objects are pretty much made for what you're describing. Using an object makes even more sense and is a nice and clean option if the data you're getting from the server is already in a suitable form (i.e. JSON). Then it's ready for you and all you have to do is parse it.

Rafael
Based on your and Chris's answers, I decided that y'all were right and started storing all the data in the object, and populating the table from that, rather than pulling data from multiple sources. My sanity has already improved somewhat.
Andrew
A: 

Set eg. each <tr> element to have a custom property eg .customData and place your javascript object there. Its a nice compromise - the data is seperated from the markup but directly related in the dom.

James Westgate
A: 

Storing the data in an object would make it easier to manage, but it would be good practice to also store a reference alongside of that to the element (makes crud easier).

Jacob Beauregard