-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Conversation
@@ -410,7 +394,7 @@ function formatValue(ctx, value, recurseTimes) { | |||
}); | |||
} | |||
|
|||
var constructor = getConstructorOf(value); | |||
var constructor = internalUtil.getConstructorOf(value); |
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.
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.
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.
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'); |
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.
The performance of Object.getOwnPropertyDescriptor
is about 17 times slower than Object.prototype.hasOwnProperty.call
. Perhaps the code from getEligibleConstructor can be used instead.
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.
That would be a breaking change for a class that defines constructor
as accessors.
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.
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?
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.
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.
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.
@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.
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.
@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.
Landed in b2870a4...061c5da. |
PR-URL: #12526 Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #12526 Reviewed-By: James M Snell <jasnell@gmail.com>
Reduces code duplication.
Checklist
Affected core subsystem(s)
url, util