views:

82

answers:

5

I have a "for" function performed in a jQuery draggable function. Is there a better way to do this from the execution time perspective? My function is:

 $("#dragger").draggable({

            containment: '#timeline',
            axis: 'x',
            drag: function(event, ui) {

            var divs = $("#timeline div.timeline");
            for (var i=0, il=divs.length; i<il; i++) {
                var layer = '#layer'+i,
                gow = layer+"Go";
                SelectClosestKeyframes(this, event, ui, $(layer), $(gow));
            }
         }      
    });

Can I improve the for (var i=0, il=divs.length; i<il; i++) Thanx

+2  A: 

http://www.websiteoptimization.com/speed/10/ is a good resource that I've used before for optimizing javascript loops.

Yellowfog
All of those techniques would be complete waste of effort here.
Pointy
Are you making a philosophical point about the futility of action in a purposeless world? Then fair enough. But the question was 'Can I improve...', not 'Will I see a benefit in improving...'. Give some credit to the questioner to know why he's asking the question.
Yellowfog
Well, it doesn't matter why he's asking the question. The loop is already pretty good, in that he stashes the length at the beginning. Compared to all the function calls, the loop mechanics are not going to be causing any perceived problem. (Also, that site is pretty darn old, so I am dubious that any of its benchmarks are relevant to modern Javascript runtimes systems.)
Pointy
I agree about the pointlessness of optimizing the loop. I did spot that for myself, you know. But if he'd gone about trying to optimize the loop he'd have learnt two things the hard way (ie. the way that sticks). One: how to optimize loops. Two: other things than loops need optimization. ALSO: I have a small child who keeps me up and makes me grumpy, sorry.
Yellowfog
+5  A: 

There's no point in trying to optimize the loop mechanics given what you're doing in the loop. First, I doubt you could get it significantly better anyway, and second because the cost of that loop code is dwarfed by the expense of all those jQuery calls.

You start off by finding a bunch of <div> elements. You then iterate over that list, but your code never uses the "divs" array at all. Instead, it does 2 more jQuery lookups (albeit fairly cheap ones, as they're based on element id) per iteration, plus whatever work is in that function.

How did you come to the conclusion that the loop needs "optimization"? Is the page slow? If so, then I'd look inside that "SetClosestKeyframes" function and see what that does. Unless there are a couple million "div.timeline" elements, your problem is not the loop itself.

Pointy
+1, Excellent answer, was about to post this. He can optimize the loop, http://blogs.sun.com/greimer/resource/loop-test.html but like you said, the loop is not the problem.
Anders
Hmm, I thought that its better to declare: var layer = '#layer'+i, gow = layer+"Go"; then var layer = '#layer'+i; var gow = layer+"Go"
Mircea
@Mircea Oh!! I didn't notice the comma! You are right, that's fine. I'll edit my answer.
Pointy
Thanx Pointy! I thought that I got it wrong...
Mircea
@Pointy, yes the function needs to be fast and in the SetClosestKeyframes can be hundreds of keyframes.
Mircea
OK, well then I think that the function is where your performance tuning should start out. Maybe you can ask another question about it!
Pointy
A: 

I would look to refactor the SelectClosestKeyframes function so it is bound to the div's with timeline class. And/or use "each".

NimChimpsky
A: 

Not that this would be any faster, just cleaner:

var dragable = this;
$("#timeline div.timeline").each(function(i) {
   SelectClosestKeyframes(dragable, event, ui, $('#layer'+i), $('#layer'+i+'Go'));
});

In fact the each() will be a little slower than your for() but on a very minute scale.

gnarf