Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs Function string interpolation vs literal vs plus
(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
eval("`" + templ + "`");
new Function
func(model);
native
z = `This is a ${model.type} interpolation example, to ${model.action} the difference between ${model.first} and ${model.second}!`;
plus
z = templ = '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):
I'll break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares four different approaches to achieve template string interpolation in JavaScript: 1. `eval()` 2. Creating a new function using `Function` constructor 3. Native string template interpolation (using backticks, template literals) 4. Manual string concatenation with the `+` operator **Options Compared** Each option is compared to measure their performance difference. **Pros and Cons of Each Approach:** 1. **eval()**: Evaluates a string as JavaScript code. * Pros: Easy to use, simple syntax. * Cons: Performance-heavy, security risks due to eval(), potential errors in the input string. 2. **Creating a new function using Function constructor**: * Pros: Can be used for more complex operations, can cache results. * Cons: Overheads of creating a new function object, slower execution compared to native template literals. 3. **Native string template interpolation (backticks or template literals)**: * Pros: Fastest and most efficient way, modern JavaScript standard. * Cons: Limited compatibility with older browsers or environments that don't support backticks or template literals. 4. **Manual string concatenation with the `+` operator**: * Pros: Simple, widely supported. * Cons: Slowest due to the overhead of creating multiple strings. **Library/Functionality Used** In this benchmark, two libraries/functions are used: 1. The `Function` constructor is used to create a new function that returns the interpolated string. 2. Backticks (`) and template literals are used for native string interpolation. **Special JS Feature/Syntax** The benchmark utilizes modern JavaScript features such as backticks (template literals) which was introduced in ECMAScript 2015 (ES6). It also uses the `+` operator with concatenation, which is a more traditional way of achieving string concatenation. **Other Alternatives** If you want to achieve template string interpolation without using modern JavaScript features like backticks or template literals, you can use other approaches such as: * Using regular expressions with replace() method * Utilizing string formatting libraries (e.g., moment.js) * Creating a custom function for simple string concatenation However, these alternatives might be less efficient and may not offer the same level of convenience and readability as native template literals or backticks.
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 Function
Comments
Confirm delete:
Do you really want to delete benchmark?