Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.parse vs String.substring for fetching one value
(version: 0)
is it faster to get one value by JSON.parse() or directly via String.substring
Comparing performance of:
JSON.parse() vs String.substring
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ob = `{"aenderungen":[],"_id":"64c0fc4abd2c8bd76c9553f2","name":"Maxis","geburtsdatum":"2023-08-02T00:00:00.000Z","ICD10Code":"k k k k k ","position":0,"empfangsdatum":"2023-07-26T10:58:18.716Z","hinzugefügtVon":"6498bf3b2afb897aa26d18e6","infos":{"berichte":"ggg","puls":"hallo yazn "},"positionarr":[0,0],"stationen":[],"__v":0}`
Tests:
JSON.parse()
var value = JSON.parse(ob)._id;
String.substring
const value = ob.substring(25,49);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.parse()
String.substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.parse()
2022833.6 Ops/sec
String.substring
190171232.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks! The provided JSON represents a benchmark test case on MeasureThat.net, which compares the performance of two different approaches: `JSON.parse()` and `String.substring` for fetching a specific value from a JSON object. **Benchmark Definition** The test aims to determine whether it is faster to extract a value using `JSON.parse()` or directly via `String.substring`. The benchmark definition is as follows: * Prepare a sample JSON object `ob` * Use `JSON.parse(ob)` to parse the JSON and extract the `_id` property * Compare the performance of this approach with another one: + Extract the 25th to 49th character of the string using `String.substring(25, 49)` **Options Compared** Two options are compared: 1. **JSON.parse()**: This method parses a JSON string and returns a JavaScript object. 2. **String.substring()**: This method extracts a section of a string. **Pros and Cons** * **JSON.parse():** + Pros: - Easier to read and maintain, as it uses a more human-readable format - Can be used with other JSON-related functions (e.g., `JSON.stringify()`) + Cons: - May have performance overhead due to the parsing process - May not be suitable for large or complex JSON objects * **String.substring():** + Pros: - Highly optimized for performance, as it directly accesses the string data - Can be used with other string-related functions (e.g., `String.indexOf()`) + Cons: - Less readable and maintainable, as it uses a more verbose syntax **Library** In this benchmark, no specific library is required or utilized. The test focuses solely on the performance comparison between two built-in JavaScript methods. **Special JS Feature/Syntax** None of the provided code utilizes special JavaScript features or syntax that would be unfamiliar to most software engineers. **Other Considerations** When considering these approaches, you may also want to think about: * **Memory usage**: `JSON.parse()` can create a new object in memory, which might impact performance and memory usage for large JSON objects. * **Readability and maintainability**: The choice of approach should be balanced with the need for easy understanding and modification of the code. **Other Alternatives** If you're interested in exploring alternative approaches or methods for extracting values from JSON objects, consider: * **DOM traversal**: Using DOM methods (e.g., `document.getElementById()`, `getElementsByClassName()`) to access JSON data * **JSON APIs**: Utilizing modern JSON APIs (e.g., `fetch()` with JSON response handling) for fetching JSON data Keep in mind that these alternatives might not be as optimized or performant as the built-in methods used in this benchmark.
Related benchmarks:
substr vs JSON.parse
JSON.parse vs string.split small fixed array
JSON.parse vs string.split 2
structuredClose(myObject) vs JSON.parse(JSON.stringify(myObject))
Comments
Confirm delete:
Do you really want to delete benchmark?