When I utilize AJAX to load a script file and run its content, my approach involves the following:
new Function('someargument',xhr.responseText)(somevalue);
However, MDN states the following:
Function
objects created with theFunction
constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.
To me, this concept is a bit puzzling. Even if a function is explicitly declared, it still needs to be parsed from the string format of the file, so why would using new Function
on a loaded string be considered inefficient?
This issue stirs more curiosity in me rather than concern. While I understand the complications that come with re-parsing the same string in a loop, for a scenario like this one, should there be any significant drawbacks?