Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RPC eval vs JSON parse.
(version: 0)
Compare function invocation using eval or JSON parse.
Comparing performance of:
eval vs parse
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
my = new Object(); my.doit = function(a, b, c) { return a + b + c; }
Tests:
eval
eval("my.doit(1, 2, 3)")
parse
var message = JSON.parse('["doit", [1, 2, 3]]'); my[message[0]](...message[1])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
parse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
4958117.5 Ops/sec
parse
1266813.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and some considerations for each approach. **Benchmark Overview** The benchmark compares two approaches to invoke the `my.doit` function: using `eval()` or parsing JSON data. The test is designed to measure which method is faster. **Benchmark Definition** The benchmark definition contains two main parts: 1. **Script Preparation Code**: This code defines an object `my` with a method `doit`. The method takes three arguments and returns their sum. 2. **Html Preparation Code**: This field is empty, indicating that no HTML code needs to be prepared for the test. **Individual Test Cases** There are two test cases: 1. **eval** * Benchmark Definition: Invokes the `my.doit` function using `eval()`. * Purpose: Measure the time taken by `eval()` to execute the `my.doit` function. 2. **parse** * Benchmark Definition: Parses a JSON string containing the method name and arguments, then invokes the corresponding method on the `my` object. * Purpose: Measure the time taken by parsing JSON data to execute the `my.doit` function. **Options Compared** The benchmark compares two approaches: 1. **eval**: Uses the built-in JavaScript `eval()` function to parse and execute the code as a string. 2. **parse**: Parses a JSON string using the `JSON.parse()` function, then uses the resulting object to invoke the method on the `my` object. **Pros and Cons** * **eval()** + Pros: - Convenient for simple one-time invocations - Easy to use + Cons: - Can be vulnerable to security risks due to its ability to execute arbitrary code - May have performance issues with large or complex code blocks * **parse** + Pros: - Safer and more secure than `eval()` - Can handle larger code blocks without performance degradation + Cons: - Requires additional parsing steps, which can be slower **Library Usage** In the `parse` test case, the `JSON.parse()` function is used to parse a JSON string. The purpose of this library is to convert a JSON-formatted string into a JavaScript object. **Special JS Features/Syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** Some alternative approaches could be explored, such as: 1. **String.prototype.eval()**: Some older versions of JavaScript used `String.prototype.eval()` to evaluate a string as code. 2. **Function constructor**: Using the `Function` constructor to create a new function from a string can also achieve similar results. However, these alternatives might not offer significant performance benefits or security advantages over the original approaches being compared in this benchmark. Please let me know if you'd like me to clarify anything further!
Related benchmarks:
JSON.parse() vs. eval()
eval vs json parse only
eval vs json parse only, no var declaration
eval vs json parse only, no var declaration (identical input)
Comments
Confirm delete:
Do you really want to delete benchmark?