OZONE Asylum
Forums
DHTML/Javascript
Optimising JavaScript loops
This page's ID:
29314
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
There are in general two kinds of optimisation in computer science: Either you don't do the work at all (or at least, you don't do *as much* work...) or you do the work ahead of time, and just recall the result when you need it instead of calculate it when you need it. The key to optimising any language is figuring out when and how you can do less work, or do the work ahead of time. JavaScript can't optimise in a lot of ways that lower level languages do - there is no integer type, there are no hard arrays, there are no pointer manipulations etc. One example is that bitshifting is commonly faster than multiplication for factors of 2 in most languages. JavaScript has only floating point numbers, but it does have bitshifting. So, in order to perform a bitshift JavaScript has to convert a floating point number to an integer, then perform the bitshift, and then convert it back again. Doing all those conversions means that bitshifting is an example of an optimisation that rarely, if ever, leads to any speedup of script. (The JavaScript version of Duff's device is one example where bitshifting actually improves performance in JavaScript, in some browsers.) A JavaScript version of Duff's device can speed up JavaScript, but really, it's seldom JavaScript that is the problem. Most often, JavaScript performance problems come from the DOM side, with a few exceptions such as string concatenations with huge amounts of strings. The general tip I'd give is: test doing things in different ways in different browsers. For example, the only way make string concatenation really fast in iew is rather slow compared to the other ways of concatenating strings in moz. What always works, however: - Cache everything you can when you first use it. Even if that use is for example in the condition of a for loop, and use the cached variable instead of doing the work of getting it again. - Do less work, use better algorithms etc. Maybe even change the HTML code a bit, that can sometimes really speed up DOM scripting. -- var Liorean = { abode: "[sigrotate][url]http://liorean.web-graphics.com/[/url]|[url]http://codingforums.com/[/url]|[url]http://web-graphics.com/[/url][/sigrotate]", profile: "[url]http://codingforums.com/member.php?u=5798[/url]"};
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »