isType performance enhancements #2886
Merged
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.
After completing #2884 and while working on #2878, I realized that I might have somewhat pessimized path evaluation by calling
_.isArray
more often through_.toPath
. Coincidentally, I also noticed that the internaltagTester
was needlessly recomputing the[object Type]
tag on every invocation. Since the isType functions are used everywhere, I decided to offset the_.toPath
pessimization with an optimization across the board.I then revisited the benchmarks discussed from #2840 (comment) onwards, because I suspected the tradeoff for the optimization in
_.isEmpty
might have changed. Indeed, it had;_.isEmpty({})
still took about three times as much time without the optimization than with it, but in Node.js,_.isEmpty('')
took five times as much time with the optimization than without it. I was then able to establish a good compromise between the optimized and the unoptimized performance profiles, by making the optimization itself more efficient. This evaluatesobj.length
only once instead of twice, which keeps the intended effect of the optimization while also avoiding the performance penalty for strings in Node.js.This PR is just to leave a trace. The weight does not change significantly and performance is likely to improve for applications that call isType functions inside hot loops. If the CI checks pass, I'll merge this without waiting for a review. Nevertheless, a review by a spontaneous volunteer is still welcome.