Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs Function string interpolation vs
(version: 0)
Compares how eval() compares to Function in some form of templating
Comparing performance of:
eval vs new Function vs native
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var templ = "This is a ${model.type} interpolation example, to ${model.action} the difference between ${model.first} and ${model.second}!"; var model = {type: "string", action: "show", first: "eval", second: "Function"}; var func = new Function("model", "return `" + templ + "`;"); var z;
Tests:
eval
z=eval("`" + templ + "`");
new Function
z=func(model);
native
z=`This is a ${model.type} interpolation example, to ${model.action} the difference between ${model.first} and ${model.second}!`
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
1636755.5 Ops/sec
new Function
3891729.5 Ops/sec
native
3946962.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare the performance of three approaches: `eval()`, `Function` (in this case, used with template literals), and "native" string interpolation (using backticks). **Options Compared** 1. **`eval()`**: Evaluates a string as JavaScript code. 2. **`Function`**: Creates a new function using the provided string template literal. 3. **Native String Interpolation**: Uses backticks (`) to insert values into a string. **Pros and Cons of Each Approach** 1. **`eval()`**: * Pros: Dynamic, flexible, and can be used to execute arbitrary JavaScript code. * Cons: Performance is generally slower due to the overhead of parsing and executing the string as code. Additionally, it's vulnerable to security risks if not properly sanitized. 2. **`Function`**: * Pros: Can be more efficient than `eval()` for simple use cases, as it avoids the overhead of parsing and executing a string as code. It also provides better type safety and error handling. * Cons: Requires creating a function object with a specific signature, which can lead to overhead if not optimized correctly. 3. **Native String Interpolation**: * Pros: Typically fast and efficient, as it involves simple string concatenation or template literal expansion. * Cons: Limited flexibility compared to `eval()` or `Function`, as it's designed specifically for string interpolation. **Library and Special JS Features** The benchmark uses the following library: 1. **Template literals**: A feature introduced in ECMAScript 2015, which allows embedding expressions inside strings using backticks (`). No special JavaScript features are used beyond template literals. **Other Considerations** When choosing between these approaches, consider the trade-offs between performance, flexibility, and security. * If you need to execute arbitrary code or have complex logic, `eval()` might be too slow. * For simple string interpolation, native string interpolation or `Function` might be a better choice if it aligns with your use case. * Keep in mind that template literals are designed specifically for string interpolation and provide better type safety than `Function`. **Alternatives** If you're interested in exploring alternative approaches, consider the following: 1. **String.prototype.replace()**: Can be used for basic string manipulation and interpolation. 2. **Internationalized String Interpolation (ISI)**: A newer feature that allows using Unicode characters and expressions in strings. 3. **ES6's String Methods**: Various methods like `split()`, `substring()`, and `replace()` can be used for string manipulation, but might not offer the same level of flexibility as template literals. Keep in mind that each approach has its strengths and weaknesses, and choosing the right one depends on your specific use case.
Related benchmarks:
eval vs new Function
eval vs new Function v3
eval vs new Function (fix)
window.eval vs new Function
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?