Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Optional chaining vs manual chaining vs loop chaining vs Try/Catch
(version: 0)
Comparing performance of:
Optional chaining vs Manual chaining vs Loop chaining vs Try/Catch
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
Optional chaining
const dataObject = {some: {deep: {hidden: {value: 1}}}}; for (let temp, i = 0; i < 1000; i +=1) { temp = dataObject?.some?.deep?.hidden?.value; temp = dataObject?.some?.deep?.black?.magic; }
Manual chaining
const dataObject = {some: {deep: {hidden: {value: 1}}}}; for (let temp, i = 0; i < 1000; i +=1) { temp = dataObject && dataObject.some && dataObject.some.deep && dataObject.some.deep.hidden && dataObject.some.deep.hidden.value; temp = dataObject && dataObject.some && dataObject.some.deep && dataObject.some.deep.black && dataObject.some.deep.black.magic; }
Loop chaining
const dataObject = {some: {deep: {hidden: {value: 1}}}}; function get(obj, steps) { let value = obj; for (let i = 0, l = steps.length; i < l; i += 1) { if (value && steps[i] in value) { value = value[steps[i]]; continue; } return undefined; } return value; } const path1 = ['dataObject', 'some', 'deep', 'hidden', 'value']; const path2 = ['dataObject', 'some', 'deep', 'black', 'magic']; for (let temp, i = 0; i < 1000; i +=1) { temp = get(dataObject, path1); temp = get(dataObject, path2); }
Try/Catch
const dataObject = {some: {deep: {hidden: {value: 1}}}}; for (let temp, i = 0; i < 1000; i +=1) { try { temp = dataObject.some.deep.hidden.value; temp = dataObject.some.deep.black.magic; } catch(e) {} }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Optional chaining
Manual chaining
Loop chaining
Try/Catch
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Optional chaining
3508702.0 Ops/sec
Manual chaining
3479927.0 Ops/sec
Loop chaining
490643.6 Ops/sec
Try/Catch
111.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of four different approaches to access nested properties in an object: 1. **Optional Chaining**: This method uses the nullish coalescing operator (`??`) to access nested properties. 2. **Manual Chaining**: This method uses a series of `&&` operators to chain together property accesses. 3. **Loop Chaining**: This method uses a loop to iterate through an array of property names and access each one individually. 4. **Try/Catch**: This method wraps the property access in a try-catch block to catch any errors. **What is being tested?** The benchmark measures the performance of each approach in terms of the number of executions per second. The goal is to determine which approach is fastest. **Options compared: Pros and Cons** 1. **Optional Chaining (??)**: * Pros: More concise, safer from null pointer exceptions. * Cons: May be slower due to additional overhead. 2. **Manual Chaining (&&)**: * Pros: More control over the access flow, potentially faster in some cases. * Cons: Longer and more error-prone code, vulnerable to null pointer exceptions. 3. **Loop Chaining**: * Pros: Can be highly customized, potentially efficient for complex accesses. * Cons: May be slower due to loop overhead, less concise code. 4. **Try/Catch**: * Pros: More robust error handling, potentially safer code. * Cons: Slower due to try-catch overhead, may introduce unnecessary errors. **Library usage** There is no library mentioned in the provided benchmark definition or test cases. **Special JS features** None are explicitly mentioned. **Benchmark preparation code** The script preparation code for each benchmark case is empty, indicating that the focus is on the performance comparison of the different approaches rather than any specific setup or environment. **Alternatives** Some alternatives to these approaches include: * Using a library like Lodash or Underscore.js, which provide optimized functions for accessing nested properties. * Using a functional programming approach with higher-order functions and currying. * Implementing a custom recursive function for accessing nested properties. * Using a data-driven approach with precomputed values. However, the choice of alternative depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Observables: loops versus EventTarget
Try/Catch vs Typeof
try-catch with artificial error vs try-catch with no error
Observables: loops versus EventTarget vs extended event
Observables: loops with try/catch versus EventTarget
Comments
Confirm delete:
Do you really want to delete benchmark?