Replacing repetitively occuring loops with eval in Javascript - good or bad?
Posted
by Herc
on Stack Overflow
See other posts from Stack Overflow
or by Herc
Published on 2010-03-14T16:27:16Z
Indexed on
2010/03/14
16:35 UTC
Read the original article
Hit count: 135
eval
|JavaScript
Hello stackoverflow!
I have a certain loop occurring several times in various functions in my code. To illustrate with an example, it's pretty much along the lines of the following:
for (var i=0;i<= 5; i++) {
function1(function2(arr[i],i),$('div'+i));
$('span'+i).value = function3(arr[i]);
}
Where i is the loop counter of course. For the sake of reducing my code size and avoid repeating the loop declaration, I thought I should replace it with the following:
function loop(s) {
for (var i=0;i<= 5; i++) { eval(s); }
}
[...]
loop("function1(function2(arr[i],i),$('div'+i));$('span'+i).value = function3(arr[i]);");
Or should I? I've heard a lot about eval() slowing code execution and I'd like it to work as fast as a proper loop even in the Nintendo DSi browser, but I'd also like to cut down on code. What would you suggest?
Thank you in advance!
© Stack Overflow or respective owner