Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs Script Blob with pre create
(version: 0)
Comparing performance of:
eval vs new Function vs Script Blob
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var _url = URL.createObjectURL(new Blob(["return 2 * 3"], { type: 'text/javascript' }));
Tests:
eval
eval("2 * 3");
new Function
(new Function("return 2 * 3"))()
Script Blob
var _script = document.createElement('SCRIPT'); _script.src = _url; document.body.appendChild(_script);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
Script Blob
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark measures the performance of three different approaches to execute JavaScript code in a browser: 1. `eval` 2. `new Function` 3. `Script Blob` These approaches are compared to determine which one is the fastest. **Options Compared** * **`eval`**: Evaluates the provided JavaScript string using the `eval()` function. * **`new Function`**: Creates a new function with the provided JavaScript string as its source code and executes it using the `()` operator. * **`Script Blob`**: Creates a script element, sets its `src` attribute to the URL of a blob containing the JavaScript string, and appends the element to the document body. The script is then executed automatically. **Pros and Cons** * **`eval`**: + Pros: Simple and concise, easy to use. + Cons: Can be vulnerable to security issues (e.g., code injection), may not work in some environments (e.g., older browsers). * **`new Function`**: + Pros: More secure than `eval`, can be used in older browsers that don't support `eval`. + Cons: May be slower and more complex than `eval`. * **`Script Blob`**: + Pros: Secure, can be used to execute scripts from a blob without exposing the script's code. + Cons: Requires creating a blob object, appending the script element to the document body, which may introduce performance overhead. **Libraries and Special Features** None mentioned in the benchmark definition. The test cases use built-in JavaScript features only. **Other Considerations** * **Security**: The `eval` approach is more vulnerable to security issues than the other two approaches. * **Browser Support**: The `new Function` approach may not work in some older browsers that don't support `eval`. * **Performance Overhead**: Creating a blob object and appending a script element to the document body may introduce performance overhead. **Alternatives** Other alternatives to these approaches include: * Using a JavaScript engine like V8 or SpiderMonkey, which provide built-in support for executing scripts. * Using a library like Web Workers or Web Assembly, which provide a more secure and efficient way to execute scripts in parallel or with native code integration. * Using a framework like React or Angular, which provide their own APIs for executing scripts and managing state changes. Keep in mind that the choice of approach depends on the specific use case and requirements.
Related benchmarks:
eval vs new Function vs Script Blob
eval vs new Function (fix)
window.eval vs new Function
window.eval function vs new Function
Comments
Confirm delete:
Do you really want to delete benchmark?