3 ms·
new Function() /may/ work - but that's only if a browser actually parses the code when you pass it in to the function. Maybe a browser is lazy and only parses i
by jeresig 18y ago
new Function() /may/ work - but that's only if a browser actually parses the code when you pass it in to the function. Maybe a browser is lazy and only parses it when it's executed for the first time? (I know that's what I'd probably do.) Of course then we're back to our original problem again - being unable to find the difference between them.
To your second point: Of course eval("function(){"+code+"}" is going to be a lot slower than new Function(code) - you're constructing a closure to any external variables that are used (not to mention that using eval will have to parse the full expression and return the result - rather than with new Function which has a singular purpose).
- tlrobinson 18y agoI suppose you could wrap the code in "if(false){ }" (or make the first statement a "return" in the case of "new Function()") and actually execute it to ensure it's being parsed, but we're entering hacky territory...