Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
structuredCloneEmulation2
(version: 1)
Comparing performance of:
fori vs cur
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var cur = (obj) => { let clone; if (Array.isArray(obj)) { clone = []; for (const x of obj) { clone.push(cur(x)); } } else if (obj !== null && typeof obj === 'object') { clone = {}; for (const [k, v] of Object.entries(obj)) { clone[k] = cur(v); } } else if (ArrayBuffer.isView(obj)) { throw new TypeError("TypedArray not implemented"); } else if (obj instanceof ArrayBuffer) { throw new TypeError("ArrayBuffer not implemented"); } else if (obj != null && !['string', 'number', 'boolean'].includes(typeof obj)) { throw new TypeError("Unexpected primative type"); } else { clone = obj; } return clone; }; var fori = (obj) => { if (obj) { const objType = typeof obj; if (obj.length !== undefined && obj instanceof Array) { const clone = Array.from(obj); for (let i = 0, len = obj.length; i < len; i++) { const v = obj[i]; const vType = typeof v; if (v && vType !== 'string' && vType !== 'number' && vType !== 'boolean') { clone[i] = fori(v); } } return clone; } else if (obj !== null && objType === 'object') { const clone = { ...obj }; const keys = Object.keys(obj); for (let i = 0, len = keys.length; i < len; i++) { const v = obj[keys[i]]; const vType = typeof v; if (v && vType !== 'string' && vType !== 'number' && vType !== 'boolean') { clone[keys[i]] = fori(v); } } return clone; } else if (objType === 'string' || objType === 'number' || objType === 'boolean') { return obj; } else { throw new TypeError("Unexpected type"); } } else { return obj; } return clone; }; var data = Array.from(Array(1000)).map((x, i) => ({ i, b: [1, 2, 3, 5, 6, 7], c: 111, d: 222, e: { aa: 1, bb: 1 }, b: false, s: 'asdfaasdfaasdfasdfasdf', n: null, }));
Tests:
fori
fori(data);
cur
cur(data);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fori
cur
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition json represents two test cases: `fori` and `cur`. These test cases measure the performance of different approaches for cloning complex JavaScript objects. **Test Case 1: `fori`** The `fori` function is designed to recursively clone arrays, objects, and primitive values. It checks the type of each value in the input object or array and either clones it directly or recursively calls itself if necessary. ```javascript var fori = (obj) => { // ... } ``` **Test Case 2: `cur`** The `cur` function is designed to emulate the behavior of the `structuredClone` method, which is a new JavaScript feature that allows cloning complex objects. The `cur` function checks the type of each value in the input object or array and either clones it directly or recursively calls itself if necessary. ```javascript var cur = (obj) => { // ... } ``` **Comparison of Approaches** Both approaches (`fori` and `cur`) use a similar recursive cloning strategy. However, there are some differences: * **`structuredClone` emulation**: The `cur` function is designed to emulate the behavior of the `structuredClone` method, which is optimized for performance and can handle complex objects efficiently. In contrast, the `fori` function does not have this optimization. * **Type checking**: Both functions check the type of each value in the input object or array, but `cur` uses a more concise syntax to achieve this. * **Performance**: The `structuredClone` emulation using `cur` is likely to be faster than the recursive cloning strategy used by `fori`, especially for large inputs. **Pros and Cons** **`fori`:** Pros: * Simpler implementation Cons: * Less optimized for performance compared to `cur` **`cur`:** Pros: * Optimized for performance using the `structuredClone` method * More concise syntax for type checking Cons: * More complex implementation **Other Considerations** * **`ArrayBuffer` and `TypedArray`**: Both functions do not handle these types, as they are not implemented in JavaScript. * **Primitive values**: Both functions correctly clone primitive values (e.g., numbers, strings, booleans). * **Testing**: The benchmark measures the execution speed of both functions for different inputs. The results can help identify which function performs better under various conditions. **Alternatives** Other approaches to cloning complex JavaScript objects could include: * Using a dedicated library like `structuredClone` * Implementing a custom recursive cloning algorithm * Using a iterative approach instead of recursion * Utilizing modern JavaScript features like `JSON.parse` and `JSON.stringify` However, these alternatives may not be optimized for performance or may have different trade-offs in terms of complexity and readability.
Related benchmarks:
lodash _.cloneDeep() vs deppClone()
test for Vovan
structuredClone vs cloneSimple vs JSON.stringify large data
Deep Clone vs JSON.Stringify vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?