Rule Definition
When calling a function in a end loop, the function will be computed for each loop iteration and will decrease dramatically performances.
Remediation
Use a variable to store the result of a function and use it as the loop termination.
Violation Code Sample
var total=0;
for (var i=0;i<myfunction(count);i++){ // VIOLATION
total+=i;
}
Fixed Code Sample
var total=0;
var loopCounter = myfunction(count);
for (var i=0; i<loopCounter; i++){ // FIXED
total+=i;
}
Reference
AJAX in Action - Manning - ISBN 1-932394-61-3 page 290 CISQ OMG
Related Technologies
HTML5
Technical Criterion
CWE-1050 - Excessive Platform Resource Consumption within a Loop
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.