Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.parse vs recursive
(version: 0)
Comparing performance of:
JSON.parse vs Recursive interpolate
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function isStringOrNumber(value){ return ( value !== undefined && value !== null && !Number.isNaN(value) && (typeof value === 'string' || typeof value === 'number') ); } function recursiveReplaceTemplateStrings( obj, templateDict, ignoreFieldFunc, currentPath) { if (Array.isArray(obj)) { return obj.map((item) => recursiveReplaceTemplateStrings(item, templateDict, ignoreFieldFunc, currentPath), ); } if (typeof obj === 'object' && obj !== null) { var newObj = {}; Object.keys(obj).forEach((key) => { var value = obj[key]; var keyPath = currentPath ? `${currentPath}.${key}` : key; if (typeof value !== 'string') { // Recursively call the function for non-string values newObj[key] = recursiveReplaceTemplateStrings( obj[key], templateDict, ignoreFieldFunc, keyPath, ); return; } if (!ignoreFieldFunc?.(key, currentPath)) { var regex = /\[%([^%]+)%]/g; newObj[key] = value.replace(regex, (_, dictKey) => { var actualValue = templateDict[dictKey]; return isStringOrNumber(actualValue) ? `${actualValue}` : ''; }); return; } newObj[key] = value; }); return newObj; } return obj; } function jsonReplaceTemplateStrings( obj, templateDict, ignoreFieldFunc, currentPath, ) { var jsonStr = JSON.stringify(obj, (key, value) => { if (typeof value === 'string') { if (!ignoreFieldFunc?.(key, currentPath)) { var regex = /\[%([^%]+)%]/g; return value.replace(regex, (_, dictKey) => { const actualValue = templateDict[dictKey]; return isStringOrNumber(actualValue) ? "" + actualValue : ''; }); } } return value; }); return JSON.parse(jsonStr); } var ignoreFieldFunc = (key, path) => { const nonInterpolatedFields = ['id', 'type', 'data.id']; return nonInterpolatedFields.some((val) => val === key || val === `${path}.${key}`); }; var obj = { id: 'some_id_[%some_id_part%]', type: 'flex', title: 'Hello, [%user_name%]!', data: { id: 'some_nested_id_[%some_nested_id_part%]', options: [{ id: 'some_nested_array_id_[%some_nested_array_id_part%]', text: 'option [%some_option_part%]', }, ], }, }; var templateDict = { some_id_part: 'unexpected_id_part', some_nested_id_part: 'unexpected_nested_id_part', some_nested_array_id_part: 'unexpected_nested_array_id_part', user_name: 'Jane', some_option_part: 'A', }; var expected = { id: 'some_id_[%some_id_part%]', type: 'flex', title: 'Hello, Jane!', data: { id: 'some_nested_id_[%some_nested_id_part%]', options: [{ id: 'some_nested_array_id_[%some_nested_array_id_part%]', text: 'option A' }], }, };
Tests:
JSON.parse
jsonReplaceTemplateStrings(obj, templateDict, ignoreFieldFunc)
Recursive interpolate
recursiveReplaceTemplateStrings(obj, templateDict, ignoreFieldFunc)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.parse
Recursive interpolate
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 121 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.parse
77162.5 Ops/sec
Recursive interpolate
161264.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Definition:** The benchmark measures the performance of two approaches: 1. `jsonReplaceTemplateStrings`: This function takes an object, a template dictionary, and an ignore field function as input. It recursively traverses the object, replacing template strings with their corresponding values from the dictionary. 2. `JSON.parse` : This function parses a JSON string into a JavaScript object. **Options being compared:** The benchmark compares two options: 1. Recursively traversing the object using a custom `recursiveReplaceTemplateStrings` function. 2. Using the built-in `JSON.parse` method to parse a JSON string. **Pros and Cons of each approach:** **Recursive approach:** Pros: * More flexible, as it allows for arbitrary template strings and dictionary values. * Can handle complex data structures. Cons: * May be slower due to the recursive function calls. * Requires more memory for storing the temporary objects during recursion. **JSON.parse approach:** Pros: * Faster, as it leverages the optimized implementation of `JSON.parse` in JavaScript engines. * More efficient memory usage, as it only requires a single JSON string to parse. Cons: * Limited flexibility, as it only works with literal JSON strings and cannot handle custom template strings or dictionary values. **Other considerations:** * The benchmark uses a specific ignore field function, which might affect the results. This function ignores fields that contain certain keywords (`id`, `type`, etc.). If this is not relevant to the test case, adjusting or removing it could be beneficial. * The benchmark does not account for potential errors in parsing the JSON string, such as malformed syntax or missing values. **Library used:** The `JSON.parse` function is part of the JavaScript standard library. No additional libraries are required for this benchmark. **Special JS feature or syntax:** There is no specific special JS feature or syntax being tested in this benchmark. The focus is on comparing two different approaches to handling template strings and JSON parsing. In summary, the benchmark tests the performance difference between recursively traversing an object with custom template strings and using the built-in `JSON.parse` method for parsing a JSON string.
Related benchmarks:
JSON.stringify vs structuredClone vs simple deepCopyObject
JSON.stringify vs structuredClone vs simple deepCopyObject 2
JSON.stringify vs structuredClone vs simple deepCopyObject 3
JSON.stringify vs structuredClone vs simple deepCopyObject 4
JSON.stringify vs structuredClone vs simple deepCopyObject 5
Comments
Confirm delete:
Do you really want to delete benchmark?