views:

84

answers:

4

I have a design question. I have a website where users enter short messages and they are displayed to other users. Sometimes these messages have formatting in them so I need to manipulate strings. I can do this either on the server or on the client. My question is where should it occur?

If it happens on the server then there's more of a load on the server as well as more data to push to the client. However, the server machine will usually be a "better" machine than what the users have.

If it happens on the client the string manipulation gets offset to the users but I'm not sure how efficient javascript/jquery string manipulation is.

Any thoughts on this?

+2  A: 

I can't imagine a "short message" that would take so much string manipulation that a client machine would be noticeably inferior to a server. Put it on the client.

Donnie DeBoer
It didn't seem significant at first. But after having tested it, if I do the string manipulation on the server it takes over a second for the data to be received by the user. If I don't do any string manipulation it takes roughly half the time. (in this case string manipulation is done on insert and when reading no manipulation needs to be done)This is based on eyeballing results with firebug.
codette
If it takes that long on the server (which is probably not significantly more powerful than a home pc), then you should definitely spread the load by doing it on the client.
Donnie DeBoer
A: 

Depending on your definition of short (>1024 characters works for me) you would have to be turning through a LOT of them for there to be significant to your server.

That said...the fewer round trips you can make from your client to the server the better. Try to do it in JavaScript first. Then move it to the server if you have to.

Chris Brandsma
A: 

Well I am not sure how you are getting this string to the other users but I am assuming you sending it back to your server anyways so the question is, should you get it clean on the client and then send it to the server or just take whatever you get and clean it on the server.

If you can guarantee javascript is enabled then offloading it to the client would really cut down a lot of extra mini processes on the server with each call and distribute that load to the client which is great.

Things to consider... if you do it just in javascript you have no guarantee of what you are getting since you are not checking / cleaning the string on the server. This could be dangerous depending on how you are sending this information to other users. Just think of javascript injection attacks.

If you do it on the server then you don't really have to worry about it because you can guarantee what you are sending out is exactly what you want instead of just passing through whatever and hoping the javascript on the client has you covered.

Are you really doing so many transactions that the string manipulation is that much of a factor compared to whatever other operations you are doing with each message server side?

Kelsey
A: 

I'd be tempted to keep it on the client side as much as possible. I might even go ahead and have client-side code compact / extract the messages as they arrive to save my web service or whatever is handing off the messages some bandwidth.

Darth Continent