Eval Vs. Script Tag (v2)

Revision 2 of this benchmark created on


Description

If for some reason you find yourself with some JavaScript in a string that you want to add, which is faster?

Setup

var theJSString = 'var somethingForTheLoopToDo=0;var longLoop=500;while(longLoop--){somethingForTheLoopToDo+=longLoop;}';

Test runner

Ready to run.

Testing in
TestOps/sec
Script Tag
var aNewElement;
var doIt = 100;
while (doIt--) {
  aNewElement = document.createElement('script');
  aNewElement.innerHTML = theJSString;
  document.head.appendChild(aNewElement);
}
ready
Eval
var doItToo = 100;
while (doItToo--) {
  window.eval(theJSString);
}
ready
Script Tag w/ Async
var aNewElement;
var doIt = 100;
while (doIt--) {
  aNewElement = document.createElement('script');
  aNewElement.setAttribute('async', true);
  aNewElement.innerHTML = theJSString;
  document.head.appendChild(aNewElement);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.