/    Sign up×
Community /Pin to ProfileBookmark

Code Optimization

I’m slightly new to JavaScript (4-5 months), but not to programming/scripting in general (7-8 years), and am working on scripts for my companies website. I was curious if anybody knew of good sites which had basic rules for JavaScript optimization, or if there were certain functions I should avoid using over others because of overhead?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 02.2005 — JavaScript runs on somebody else's computer, so optimization isn't such a big deal. But you do want to be polite when you are a guest.

Array.prototype.sort has a huge overhead, but you knew that. The exec function runs things through the parser again and is almost never called for. But the big thing to remember is that it is an interpreted language with automatic garbage collection. Every line of code and every bit of namespace is precious. Take the time to really learn the language; if you find yourself thinking that there must be a better way then you can expect that there is. And any script that is longer than a dozen lines is too long.

The more important thing to keep in mind is that only one in ten users use JavaScript. You must ensure that the page is still usable when JavaScript is "turned off or not supported."
Copy linkTweet thisAlerts:
@acorbelliauthorApr 02.2005 — 
...any script that is longer than a dozen lines is too long.

The more important thing to keep in mind is that only one in ten users use JavaScript. You must ensure that the page is still usable when JavaScript is "turned off or not supported."[/QUOTE]


I'm not sure that I could compress some of my scripts into a dozen or fewer lines...I'll take a look at them.

My scripts are all non-mission-critical. Nothing I'm running affects the site other than by being an optional way to view things, my 'main' (read, most used) script makes leaves, snow, or happy suns fall gently on the side of the screen and my other script works on a set of five images to provide image flipping and resizing as necessary.

I think that the first one is slightly annoying after a while, but my boss likes it so it stays...and the second one uses a little flair (the thumbnails move 'into' the viewing pane as if it were being dragged and dropped) and will (in the future) use cookies to set preferences (whether the user wants animation, simple flips, or no javascript at all, default being simple flips with no animation).

Anyways, I'm just looking to make sure I avoid the largest overhead functions and methods if I can get around them.

Thanks for your reply!
Copy linkTweet thisAlerts:
@JPnycApr 02.2005 — And don't use eval.
Copy linkTweet thisAlerts:
@PalrosApr 02.2005 — And don't use eval.[/QUOTE]

I do think that many uses of eval are almost completely useless, but that doesn't mean you shouldn't use it. I've seen it used for javascript calculators, and it's kind of important there, but it is completely unnecessary to create strings that are dom calls and then eval them. Never seen the point. You could just use methods that take string arguments instead, such as getElementById and getElementByName, or just use []. And if you want to eval a string to change it to a variable, you can just use window["variablename"] to get it. instead. To change a String to a number, you can easily use parseInt and parseFloat. Also try to avoid using document.all, when you can use document.getElementById and have the same effect.

here is a good tutorial about script optimization.

[URL]http://www.javascriptkit.com/javatutors/efficientjs.shtml[/URL]
×

Success!

Help @acorbelli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...