Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Own vs jsonParse
(version: 0)
Comparing performance of:
Json parse-stringify vs own one
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { "problems": [{ "Diabetes":[{ "medications":[{ "medicationsClasses":[{ "className":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }], "className2":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }] }] }], "labs":[{ "missing_field": "missing_value" }] }], "Asthma":[{}] }]}; var obj2 = null;
Tests:
Json parse-stringify
var obj2 = JSON.parse(JSON.stringify(obj));
own one
var obj2 = deepCopyData(obj); function deepCopyData(data) { let node; if (Array.isArray(data)) { node = data.length > 0 ? data.slice(0) : []; node.forEach((e, i) => { if ((typeof e === 'object') || (Array.isArray(e) && e.length > 0)) { node[i] = deepCopyData(e); } }); } else if (data && typeof data === 'object') { node = Object.assign({}, data); Object.keys(node).forEach((key) => { if ((typeof node[key] === 'object') || (Array.isArray(node[key]) && node[key].length > 0)) { node[key] = deepCopyData(node[key]); } }); } else { node = data; } return node; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Json parse-stringify
own one
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 provided benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to create a copy of an object in JavaScript: 1. **JSON.parse(JSON.stringify(obj))**: This method uses the `JSON` object to parse and reconstruct the original object. 2. **Own implementation (deepCopyData)**: A custom function, `deepCopyData`, is used to recursively clone the object. **Options Compared** The two options are compared in terms of: * Performance: The time it takes to execute each benchmark * Memory usage: The amount of memory required by each benchmark **Pros and Cons of Each Approach** 1. **JSON.parse(JSON.stringify(obj))**: * Pros: + Easy to implement and understand + Works well for simple objects with no nested references * Cons: + Can be slow and inefficient for large or complex objects due to the overhead of parsing and reconstructing the object graph + May not work correctly if the original object contains circular references or other special characters 2. **Own implementation (deepCopyData)**: * Pros: + More efficient and scalable than JSON.parse(JSON.stringify(obj)) for large or complex objects + Can handle circular references and other edge cases more accurately * Cons: + Requires custom implementation and understanding of the algorithm + May be slower to implement and test compared to JSON.parse(JSON.stringify(obj)) **Library: JSON** The `JSON` object is used in the first approach (JSON.parse(JSON.stringify(obj))) to parse and reconstruct the original object. The `JSON` object provides a standardized way to represent JavaScript objects as text and vice versa. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** Besides the two approaches mentioned, there are other methods for creating copies of objects in JavaScript, such as: * Using `Object.assign()` with an empty object (e.g., `const obj2 = Object.assign({}, obj);`) * Using `Array.prototype.slice()` and `JSON.parse(JSON.stringify(obj))` together (e.g., `const obj2 = JSON.parse(JSON.stringify(obj.slice()));`) * Using a library like Lodash's `cloneDeep()` function However, these alternatives are not being tested in this specific benchmark.
Related benchmarks:
Loop perf
array some vs _.some III
find vs includes on object
Lodash.isEqual vs JSON.stringify Equality Comparison for Large Deep Object.
json stringify vs string...ssss
Comments
Confirm delete:
Do you really want to delete benchmark?