Skip to content

isType performance enhancements #2886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2020
Merged

Conversation

jgonggrijp
Copy link
Collaborator

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 internal tagTester 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 evaluates obj.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.

In Node.js 10 on a 2012 Mac Mini, this reduces the time of a single
call to _.isString and similar functions from 192 ns to 63 ns.
After optimizing the internal tagTester function, I decided to re-
evaluate the optimization and benchmark discussed from

jashkenas#2840 (comment)

onwards. Now that tagTester is faster, the tradeoff posed by this
optimization had become less obviously in favor. Especially evaluating
the length of strings appeared to be relatively costly. I realized
this was happening twice, so I removed the redundant evaluation. This
could be done at a net zero weight change.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 95.175% when pulling 3730dfc on jgonggrijp:stringtag-perf into 1964cdb on jashkenas:master.

@jgonggrijp jgonggrijp merged commit ee99923 into jashkenas:master Oct 24, 2020
@jgonggrijp jgonggrijp deleted the stringtag-perf branch October 24, 2020 23:22
jgonggrijp added a commit that referenced this pull request Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants