Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs Function string interpolation vs pl vs native
(version: 0)
Compares how eval() compares to Function in some form of templating
Comparing performance of:
eval vs new Function vs native vs plus
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}!`;
plus
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 (4)
Previous results
Fork
Test case name
Result
eval
new Function
native
plus
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):
The provided JSON represents a JavaScript benchmark that compares the performance of different approaches to achieve templating in a string. **Options being compared:** 1. `eval()`: The `eval()` function is used to execute a string as JavaScript code. 2. `Function`: The `Function` constructor is used to create a new function with a given name and arguments. 3. `native`: This approach uses template literals (the backtick syntax) to achieve templating in a string. 4. `plus`: This approach concatenates strings using the `+` operator. **Pros and Cons of each approach:** 1. **eval()**: * Pros: Can be used for simple, one-time use cases where performance isn't critical. * Cons: Can pose security risks if not sanitized properly, as it executes arbitrary code. 2. **Function**: * Pros: Can be more efficient than `eval()` when used correctly, as it avoids the overhead of parsing and executing code at runtime. * Cons: Requires creating a new function object, which can be slower for simple use cases. 3. **native** (template literals): * Pros: Fastest approach, as it avoids parsing and executing code at runtime. * Cons: Limited to use with modern JavaScript engines that support template literals. 4. **plus**: * Pros: Simple and easy to implement, but slower than `native` due to string concatenation overhead. **Library usage:** None of the test cases use any external libraries. **Special JS feature or syntax:** The benchmark uses template literals (the backtick syntax) in the `native` approach. This is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other considerations:** * The `Function` constructor can be vulnerable to code injection attacks if not used carefully. * The `eval()` function should only be used when the input string is trusted, as it executes arbitrary code. **Alternative approaches:** 1. **String interpolation**: Some libraries like Handlebars or Pug provide built-in support for templating in strings. 2. **Template engines**: Separate template engines like Mustache or Nunjucks can be used to achieve templating in a more structured way. 3. **String manipulation functions**: Some browsers have built-in string manipulation functions like `String.prototype.replace()` or `String.prototype.format()`, which can be used for simple templating needs. Keep in mind that the best approach depends on the specific use case and performance requirements.
Related benchmarks:
eval vs new Function
eval vs new Function v3
eval vs new Function (fix)
window.eval function vs new Function2
eval vs new Function vs native
Comments
Confirm delete:
Do you really want to delete benchmark?