Javascript: How much is too much?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Javascript: How much is too much?

Post by Chalks »

I'm building a web app that uses significantly more javascript than I've ever used before.

How can I accurately test the speed at which the javascript executes?
Or, how fast the page loads?
Are there any rough guidelines for how much javascript is "too much"?
I know almost nothing about optimizing code (especially javascript) for speed, any pointers?

Obviously all of this will differ based on browsers used, internet speeds of the user, and other things that I'm not thinking of, so I'll be testing on several different browsers, OSs, and internet speeds. Whew, I always end up with tons of questions when I start a new project.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Javascript: How much is too much?

Post by pickle »

  1. You can use the Javascript Date() object to get timestamps - much like you would in PHP
  2. Set one timestamp with PHP & output it as a Javascript variable. Set a second timestamp with Javascript once the document has loaded. Do some math & voila!
  3. "Too much" is purely subjective. I have a feeling a lot of people will tell you 1) it's too much when it makes the user experience less enjoyable 2) it's too much if the site requires javascript. I think the second point can be argued either way - depending on what your project is.
  4. "Big O" notation is useful for determining how expensive (clock-cycle-wise) an operation, snippet, function, whole program is. Due to different optimizations in Javascript engines though, your best bet might be to build 2 solutions to a problem & see what runs faster.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Javascript: How much is too much?

Post by omniuni »

If you're building a javascript heavy website, I would recommend you at least test it in the few major JS engines, although the list is growing.

1. The two biggest ones are IE and Mozilla
2. Opera and Safari/Konqueror are next in line
3. Google Chrome may be a very good idea to test in since its JS engine will likely be the future engine for Mozilla and KDE browsers.

As long as the site operates well in at least the browsers mentioned in 1 and 2, you're probably good to go. Still, if the JS is complex enough that a poor connection to the server may cause problems, create some way to give the user an alert or deal with the loss of connectionn. My webmail and Facebook both do this, and it's a good thing too.

I would also say that if you are just using JS for animations and useless fluff... it's probably too much. Things don't always need to "slide" or "fade", just appearing and disappearing are fine, and vastly improve performance.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Javascript: How much is too much?

Post by Chalks »

pickle wrote:
  1. You can use the Javascript Date() object to get timestamps - much like you would in PHP
  2. Set one timestamp with PHP & output it as a Javascript variable. Set a second timestamp with Javascript once the document has loaded. Do some math & voila!
  3. "Too much" is purely subjective. I have a feeling a lot of people will tell you 1) it's too much when it makes the user experience less enjoyable 2) it's too much if the site requires javascript. I think the second point can be argued either way - depending on what your project is.
I knew the timing thing was simple... just didn't realize it was that simple. Thanks! As for requiring javascript, I go back and forth on it. For sites that I build for companies or other people, I never require it. However, if it's a site primarily for myself (as this project is), then I often will. Of course, I still send up a (nice looking) warning if javascript isn't enabled by the user.
pickle wrote:"Big O" notation is useful for determining how expensive (clock-cycle-wise) an operation, snippet, function, whole program is. Due to different optimizations in Javascript engines though, your best bet might be to build 2 solutions to a problem & see what runs faster.[/list]
Wow. I JUST learned about Big O notation in class (computer science major)... I can't believe I actually learned something usefull!

omniuni wrote:I would also say that if you are just using JS for animations and useless fluff... it's probably too much. Things don't always need to "slide" or "fade", just appearing and disappearing are fine, and vastly improve performance.
That's very true, and most of the things on this project do just instantly appear/disappear. However, the site feels more 'alive' with a few moving parts. That may just be my opinion, but since this is mainly for my own entertainment... it's my opinion that matters. :D
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Javascript: How much is too much?

Post by omniuni »

If entertainment is more important than speed, then animate away, and I would like to see this thing and just how many cpu cycles it takes up. :P
Post Reply