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
pref: use new.target to replace instanceof #60
Conversation
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.
lgtm
how much does this improve things? |
For the record: const max = 100000000
function InstanceOfClass () {
if (!(this instanceof InstanceOfClass)) {
return new InstanceOfClass()
}
}
function NewTargetClass () {
if (!new.target) {
return new NewTargetClass()
}
}
console.time('InstanceOfClass')
for (let i = 0; i < max; i++) {
InstanceOfClass()
}
console.timeEnd('InstanceOfClass')
console.time('NewTargetClass')
for (let i = 0; i < max; i++) {
NewTargetClass()
}
console.timeEnd('NewTargetClass') results in:
Impressive, I didn't know about this! |
If we're talking about perf, we should maybe use the fastest pattern, explicit |
Would you like to send a PR to update this? |
I just saw the mentioned above. It is really faster. const max = 100000000
function InstanceOfClass () {
if (!(this instanceof InstanceOfClass)) {
return new InstanceOfClass()
}
}
function NewTargetClass () {
if (!new.target) {
return new NewTargetClass()
}
}
function NewTargetClassUndefined () {
if (new.target === undefined) {
return new NewTargetClass()
}
}
console.time('NewTargetClass')
for (let i = 0; i < max; i++) {
NewTargetClass()
}
console.timeEnd('NewTargetClass')
console.time('InstanceOfClass')
for (let i = 0; i < max; i++) {
InstanceOfClass()
}
console.timeEnd('InstanceOfClass')
console.time('NewTargetClassWithUndefined')
for (let i = 0; i < max; i++) {
NewTargetClassUndefined()
}
console.timeEnd('NewTargetClassWithUndefined') The result is : NewTargetClass: 1.169s
InstanceOfClass: 3.983s
NewTargetClassWithUndefined: 52.08ms
|
At this point we probably shouldn't care or not put any further time into this, see #61 |
as titled