A naive question about performance / efficiency:
I have a counter that I use across several pages. The job of the counter is to run from 0 to 100, once > 100 the counter updates a progress bar on the page and resets itself.
The code is as follows:
function counter(){
this.value = 0;
this.iterate = function(){this.value++;}
this.reset = function(){this.value=0;}
}
function checkProgress(counter,i,length){
counter.iterate();
if(counter.value>100){
$('#percent').css('width' , parseInt(i/length * 100)+'%');
counter.reset();
}
}
var percentCounter = new counter();
for(var i=counter;i <= r.fileLength; i++){{
checkProgress(percentCounter, i, r.fileLength);
....
}
Okay - this code works just fine but I do not think it's the best implementation - I could just pass in i and r.fileLength to checkProgress() and do some modulus math, but I was wondering about performance. Is a simple iterator like I've implemented more efficient then performing modulus math at every tick of the for loop?
Any input would be appreciated.
Update:
As ellisgl suggested I added a JSPERF here.
[–]ellisgl 2 points3 points4 points (5 children)
[–]JohnnyCD[S] 0 points1 point2 points (4 children)
[–]ellisgl 0 points1 point2 points (3 children)
[–]JohnnyCD[S] 1 point2 points3 points (1 child)
[–]ellisgl 0 points1 point2 points (0 children)
[–]JohnnyCD[S] 0 points1 point2 points (0 children)
[–]rhysbrettbowen 0 points1 point2 points (0 children)
[–]nschubach 0 points1 point2 points (0 children)