perf: optimize Error object creation in promise.js
functions
#3169
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This merge request optimizes several functions in
promise.js
, includingping()
,execute()
,query()
, and other similar methods, by moving the creation ofError
objects inside their respective error handling blocks. This change ensures thatError
objects are instantiated only when actual errors occur, reducing unnecessary overhead during successful operations.Background and Motivation
Performance Issue:
Error
objects were being created every time these functions were called, regardless of whether an error occurred.Performance Analysis:
Error
objects only when needed reduces CPU usage and execution time.Error
object instantiation is deferred until an actual error occurs.Changes Made
Modified Functions:
ping()
execute()
query()
promise.js
exhibiting the same pattern.Optimization Details:
Moved the instantiation of
Error
objects inside theif (err)
blocks within each function.Ensured that
Error
objects are only created when an error is present.Example change:
Benefits
Performance Improvement:
Error
objects during successful operations.Scalability:
Best Practices Compliance:
Testing
Compatibility
Additional Notes
Conclusion
By optimizing the creation of
Error
objects inping()
,execute()
,query()
, and other functions withinpromise.js
, this change enhances the performance and scalability of themysql2
library. Avoiding unnecessary instantiation ofError
objects reduces CPU and memory overhead, leading to more efficient resource utilization, especially in applications with high transaction volumes.