Skip to content
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

url: use util's getConstructorOf #12526

Closed
wants to merge 2 commits into from

Conversation

TimothyGu
Copy link
Member

Reduces code duplication.

Checklist
Affected core subsystem(s)

url, util

@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x whatwg-url Issues and PRs related to the WHATWG URL implementation. util Issues and PRs related to the built-in util module. labels Apr 20, 2017
@TimothyGu
Copy link
Member Author

@@ -410,7 +394,7 @@ function formatValue(ctx, value, recurseTimes) {
});
}

var constructor = getConstructorOf(value);
var constructor = internalUtil.getConstructorOf(value);
Copy link
Contributor

@mscdex mscdex Apr 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this at the top of the file so we're not referencing internalUtil each time? Until lib/internal/util.js gets refactored to use the "new" module.exports = { ... } format, the property lookup could be slow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have done just that in 9077b48 / #11404.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, internal/util has now been refactored to use the module.exports = { ... } format.


exports.getConstructorOf = function getConstructorOf(obj) {
while (obj) {
var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance of Object.getOwnPropertyDescriptor is about 17 times slower than Object.prototype.hasOwnProperty.call. Perhaps the code from getEligibleConstructor can be used instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a breaking change for a class that defines constructor as accessors.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been asking myself in which case I'll need to do that. And I don't find the answer. Can you provide me some example of a case where you really want to do that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll answer myself: even if I don't found any case, it should be a breaking change if someone does it that way.
But remember that URL constructor always has been using hasOwnProperty and now it will use getOwnPropetyDescriptor, so it means that a -1700% performance will happen. I'm ok with that but perhaps someone is not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyGu the code currently explicitly skips over any get constructor() {} - previously in url, it only used hasOwnProperty, so as-is, this is a breaking change for URL. it's only in lib/util that this is keeping the behavior the same.

In other words, any deduplication of this code seems to be a breaking change in one place or the other.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb, the URL change that added hasOwnProperty has not been released yet. In fact, the WHATWG URL API is still in experimental phase, and so compatibility on the URL side is not an issue.

@jseijas the URL constructor is not using this function, only the custom inspection function (which is not performance-sensitive at all) is using it.

@TimothyGu
Copy link
Member Author

Landed in b2870a4...061c5da.

@TimothyGu TimothyGu closed this Apr 24, 2017
@TimothyGu TimothyGu deleted the util-inspect-constr branch April 24, 2017 23:43
TimothyGu added a commit that referenced this pull request Apr 24, 2017
PR-URL: #12526
Reviewed-By: James M Snell <jasnell@gmail.com>
TimothyGu added a commit that referenced this pull request Apr 24, 2017
PR-URL: #12526
Reviewed-By: James M Snell <jasnell@gmail.com>
@jasnell jasnell mentioned this pull request May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants