-
Notifications
You must be signed in to change notification settings - Fork 28
Conversation
Codecov Report
@@ Coverage Diff @@
## master #85 +/- ##
==========================================
- Coverage 75.23% 73.45% -1.78%
==========================================
Files 34 35 +1
Lines 432 437 +5
Branches 84 82 -2
==========================================
- Hits 325 321 -4
- Misses 89 98 +9
Partials 18 18
Continue to review full report at Codecov.
|
@@ -21,21 +21,12 @@ function createRunHook( hooks, returnFirstArg ) { | |||
* @return {*} Return value of runner, if applicable. | |||
*/ | |||
return function runHooks( hookName, ...args ) { | |||
|
|||
if ( ! validateHookName( hookName ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes all validation from hooks names when applying them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes all validation from hooks names when applying them.
What's the worst case scenario here?
The assumption I'd made, noted in extended commit description of a0e6eb6, is that validation of the hooks takes place when they're added, and if no hooks exist which match the hook name (a given if it's an invalid hook name), nothing would occur.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable 👍
@@ -12,8 +12,10 @@ import createDidHook from './createDidHook'; | |||
* @return {Object} Object that contains all hooks. | |||
*/ | |||
function createHooks() { | |||
const actions = {}; | |||
const filters = {}; | |||
const actions = Object.create( null ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear why this is changing this here, is this performance related or required for other changes here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, got it - I didn't see that this was required for the other changes to work. Nice!
Unnecessary because a hook cannot be registered with an invalid hook name, so it would not pass the subsequent condition to check that a hookset with corresponding name exists.
Even if we don't intend to return value, no harm in assigning to args[ 0 ]
71bd540
to
8d5854b
Compare
Looks good to me! |
Foreseeing
runHooks
becoming a hot code path, I took to making a few small micro-optimizations, improving the handled and unhandled hook performance by approximately 42% and 172% respectively on my machine (handled meaning at least one hook handler exists). Standard benchmarking caveats apply (Node V8 engine, machine-specific, benchmarks themselves limited in real-world applicability).Before:
After:
Optimizations include:
doAction( '__current' )
to attempt to find handlers for the internal property, though would fail after the second condition, since array would not have.handlers
property. To me, it's more concerning that we're allowing for this special key to exist with special treatment in the same scope as other hook handlers.hasOwnProperty
, faa0ed3, jsperf)Object.create( null )
is to avoid issues with prototype property access:!! ( {} ).valueOf // true
!! Object.create( null ).valueOf // false
Try yourself: