Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs Script Blob
(version: 0)
Comparing performance of:
eval vs new Function vs Script Blob
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("2 * 3");
new Function
func();
Script Blob
var _script = document.createElement('SCRIPT'); var _url = URL.createObjectURL(new Blob(["return 2 * 3"], { type: 'text/javascript' })); _script.src = _url; document.body.appendChild(_script); URL.revokeObjectURL(_url); _script.remove();
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 135 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
4581632.5 Ops/sec
new Function
83195416.0 Ops/sec
Script Blob
389.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its test cases, explaining what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares the performance of three different approaches to execute a simple JavaScript expression: 1. `eval`: Directly evaluates the expression using the built-in `eval` function. 2. `new Function`: Creates a new function object using the `Function` constructor and then calls it with the desired arguments. 3. Script Blob: Creates a script blob (a blob of JavaScript code) that can be executed by the browser without requiring an explicit function call. **Test Case 1: eval** The first test case uses the built-in `eval` function to execute the expression `"2 * 3"`. This is a simple and straightforward approach, but it may have performance implications due to security restrictions and potential overhead. Pros: * Easy to implement * Widely supported Cons: * May incur additional overhead or security restrictions * Can be slower than other approaches **Test Case 2: new Function** The second test case uses the `Function` constructor to create a new function object with the expression `"return 2 * 3"` as its body. The resulting function is then called using the parentheses `()`. Pros: * Allows for more control over the execution environment * Can be faster than `eval` Cons: * May incur additional overhead due to function creation and call * Requires explicit function call **Test Case 3: Script Blob** The third test case uses a script blob (a blob of JavaScript code) that is created using the `URL.createObjectURL` method. The script blob contains the expression `"return 2 * 3"` as its content. The script blob is then executed by setting its `src` attribute and appending it to the document body. Pros: * Allows for execution without explicit function call * Can be faster than other approaches Cons: * May incur additional overhead due to blob creation and execution * Requires knowledge of URL-based JavaScript execution **Library: None** There are no libraries used in this benchmark. However, note that some browsers may use internal libraries or engines that can affect the performance of these approaches. **Special JS Features/Syntax: None** None of the test cases rely on special JavaScript features or syntax. The focus is solely on comparing the performance of the three different execution approaches. **Other Alternatives** Some alternative approaches to executing JavaScript expressions include: * `Function.prototype.call()`: Calls a function object using its `call` method, passing in arguments. * `evalAt()`: A non-standard method that allows evaluating an expression at a specific context (not widely supported). * Just-In-Time (JIT) compilation: Some engines, like SpiderMonkey, can JIT compile JavaScript expressions for improved performance. Keep in mind that the choice of approach often depends on the specific use case and requirements.
Related benchmarks:
eval vs new Function v3
eval vs new Function #2
window.eval function vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?