3 ms·
Regarding testing JavaScript parse speed, why is it difficult to test in the browser? Couldn't you, for example, wrap a string containing JavaScript code in "fu
by tlrobinson 18y ago
Regarding testing JavaScript parse speed, why is it difficult to test in the browser? Couldn't you, for example, wrap a string containing JavaScript code in "function(){}" and pass it to eval, or do "new Function()", and not execute the result?
As a corollary to that, doing
eval("function(){"+code+"}")
is much slower (~5x) than
new Function(code)
at least in Firefox using 280slides.com as my test subject (which is part of the reason we do use "new Function").
- jeresig 18y agonew 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...