Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs Function vs lodash.template v3
(version: 0)
Comparing performance of:
eval vs Function vs lodash.template
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var model = {type: {test: "a"}, action: "show", first: "eval", second: "Function"}; var func = new Function("model", "return model.type");
Tests:
eval
eval('model.type')
Function
func(model);
lodash.template
_.template('${type}')(model);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
Function
lodash.template
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
23498422.0 Ops/sec
Function
183458640.0 Ops/sec
lodash.template
65552.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmarking test on MeasureThat.net. **Overview** The test compares three ways to access a nested property in an object: `eval()`, `Function` constructor, and `lodash.template()` (a part of the Lodash library). The test aims to determine which approach is the fastest. **What's being tested?** In the benchmarking script, we have two main variables: * `model`: an object with a nested property `type.test` * `func`: a function created using the `Function` constructor that returns the value of `model.type` The test creates three versions of this scenario: 1. `eval('model.type')`: uses the built-in `eval()` function to access the nested property. 2. `func(model)`: uses the custom-made `func` function to access the nested property. 3. `_template('${type}')(model)`: uses Lodash's `template()` function with a template string to create an interpolated value, and then applies this value to `model`. **Options compared** The three options are compared in terms of execution speed. * **eval()**: This is a built-in JavaScript function that parses the expression passed as an argument and executes it. It can pose security risks if used with untrusted input. * **Function constructor**: This creates a new function with the given arguments and body, which can be executed by calling it. In this case, we create a function `func` that returns the value of `model.type`. * **Lodash.template()**: This is a higher-order function from Lodash that takes a template string as an argument and returns a function that creates an interpolated value when called. **Pros and Cons** Here are some pros and cons for each approach: 1. **eval()**: * Pros: simple to implement, fast execution * Cons: security risks due to untrusted input, limited flexibility 2. **Function constructor**: * Pros: flexible, can be used with various data types, safe from security risks * Cons: slower execution than `eval()` and Lodash template, requires more code 3. **Lodash.template()**: * Pros: fast execution, flexible, secure, and easy to use * Cons: requires an external library (Lodash), might be overkill for simple cases **Lodash library** The Lodash library is a popular JavaScript utility library that provides a wide range of functions for various tasks, such as array manipulation, object manipulation, function currying, and more. In this case, the `template()` function is used to create an interpolated value. **Other considerations** * **Browser-specific behavior**: The test results show browser-specific performance variations. * **Execution speed vs security**: While Lodash template offers fast execution and security benefits, it requires an external library, which might not be desirable in all cases. **Alternatives** If you need to access nested properties in objects, consider the following alternatives: 1. **Bracket notation** (`model['type.test']`): a more modern and secure way to access nested properties. 2. **Destructuring assignment**: (`const { type: { test } } = model;`) for object destructuring, which can be faster than `eval()` or Lodash template. 3. **String interpolation with template literals** (``${model.type.test}``): another modern and secure way to create interpolated strings. Keep in mind that the best approach depends on your specific use case and performance requirements.
Related benchmarks:
isFunction vs typeof function 6
eval vs Function vs lodash.template
eval vs Function vs lodash.template v2
eval vs Function vs lodash.template v4
Comments
Confirm delete:
Do you really want to delete benchmark?