Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.parse() vs. eval() large object 2
(version: 1)
Comparing performance of:
JSON.parse() vs eval()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var jsonString = '{"user":{"id":1,"name":{"first":"太郎","last":"山田"},"age":30,"email":"taro.yamada@example.com","address":{"street":"1-2-3","city":"東京","prefecture":"東京都","postalCode":"100-0001"},"phoneNumbers":[{"type":"mobile","number":"090-1234-5678"},{"type":"home","number":"03-1234-5678"}],"hobbies":[{"name":"サッカー","level":"中級"},{"name":"読書","genres":["フィクション","ノンフィクション","ミステリー"]},{"name":"料理","skills":{"cuisine":["和食","イタリアン"],"level":"上級"}}],"preferences":{"language":"日本語","notifications":{"email":true,"sms":false,"push":true}}}}' var jsonString2 = '({"user":{"id":1,"name":{"first":"太郎","last":"山田"},"age":30,"email":"taro.yamada@example.com","address":{"street":"1-2-3","city":"東京","prefecture":"東京都","postalCode":"100-0001"},"phoneNumbers":[{"type":"mobile","number":"090-1234-5678"},{"type":"home","number":"03-1234-5678"}],"hobbies":[{"name":"サッカー","level":"中級"},{"name":"読書","genres":["フィクション","ノンフィクション","ミステリー"]},{"name":"料理","skills":{"cuisine":["和食","イタリアン"],"level":"上級"}}],"preferences":{"language":"日本語","notifications":{"email":true,"sms":false,"push":true}}}})'
Tests:
JSON.parse()
var dummy = JSON.parse(jsonString);
eval()
var dummy = eval(jsonString2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.parse()
eval()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.parse()
310856.7 Ops/sec
eval()
256891.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark presented compares two different methods for parsing a JSON-like string in JavaScript: `JSON.parse()` and `eval()`. ### Approaches Compared: 1. **JSON.parse()** - **Definition**: A built-in JavaScript function that parses a JSON string and constructs the corresponding JavaScript value or object. - **Test Code**: `var dummy = JSON.parse(jsonString);` - **Pros**: - **Security**: `JSON.parse()` is inherently safer as it does not execute any JavaScript code contained in the string. This makes it less vulnerable to code injection attacks. - **Performance**: Optimized for parsing valid JSON, providing better performance for typical JSON structures compared to `eval()`. - **Validity**: It only accepts valid JSON syntax, meaning malformed JSON strings will throw an error, helping to catch errors early. - **Cons**: - **Limited to JSON**: It can only parse strings formatted as JSON, which may not be flexible for certain non-JSON-like structures. 2. **eval()** - **Definition**: A JavaScript function that evaluates a string as JavaScript code. - **Test Code**: `var dummy = eval(jsonString2);` - **Pros**: - **Flexibility**: Since `eval()` executes the string as JavaScript code, it can handle any JavaScript expression, not just valid JSON. - **Cons**: - **Security Risks**: Using `eval()` poses significant security risks as it can execute potentially harmful code if the input string is derived from an untrusted source. - **Performance**: Generally slower than `JSON.parse()` for parsing JSON as it has to invoke the JavaScript interpreter and execute the code, which adds overhead. - **Debugging Difficulty**: Errors in evaluated code can be harder to track down. ### Libraries and Features Involved: - **JSON Object**: Part of the JavaScript language, which provides methods for working with JSON, including serialization (`JSON.stringify()`) and parsing (`JSON.parse()`). - The benchmark does not use any external libraries; it relies solely on built-in JavaScript functionality. ### Considerations: - **Use Case**: Developers should generally prefer `JSON.parse()` for parsing JSON due to its safety, performance, and clarity. `eval()` might only be considered in very specific contexts where dynamic execution of JavaScript is required, and the input can be trusted. - **Browser Compatibility**: Both approaches are widely supported across all modern browsers, though performance may vary based on the JavaScript engine in each browser. ### Alternatives: - **Third-Party Libraries**: For more advanced JSON operations or to enhance performance, libraries like `lodash` can be used, though they typically do not replace `JSON.parse()`. `lodash` can handle deep cloning, merging, and other data transformations which extend beyond simple parsing. - **Structured Clone**: If your goal is to clone complex objects, using the structured clone algorithm (available in Web APIs but not directly in JavaScript) may be appropriate, especially for non-JSON serializable structures. In summary, for typical JSON parsing tasks, `JSON.parse()` is the recommended approach due to its security, performance, and consistency, while `eval()` should be avoided unless absolutely necessary and only with trusted input.
Related benchmarks:
creeps filter
creeps filter
creeps filter
creeps filter
LodashBigStructure
LodashBigStructure(VM1)
lodash clonedeep vs json.parse(stringify())
json parse and search regex1
JSON.parse() vs. eval() large object
Comments
Confirm delete:
Do you really want to delete benchmark?