-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,22 +278,6 @@ function arrayToHash(array) { | |
} | ||
|
||
|
||
function getConstructorOf(obj) { | ||
while (obj) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor'); | ||
if (descriptor !== undefined && | ||
typeof descriptor.value === 'function' && | ||
descriptor.value.name !== '') { | ||
return descriptor.value; | ||
} | ||
|
||
obj = Object.getPrototypeOf(obj); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
function ensureDebugIsInitialized() { | ||
if (Debug === undefined) { | ||
const runInDebugContext = require('vm').runInDebugContext; | ||
|
@@ -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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Btw, |
||
|
||
// Some type of object without properties can be shortcutted. | ||
if (keys.length === 0) { | ||
|
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 thanObject.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 inurl
, it only usedhasOwnProperty
, 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.