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

pref: use new.target to replace instanceof #60

Merged
merged 1 commit into from Feb 18, 2022

Conversation

xtx1130
Copy link
Contributor

@xtx1130 xtx1130 commented Feb 18, 2022

as titled

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

how much does this improve things?

@mcollina
Copy link
Member

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:

InstanceOfClass: 3.741s
NewTargetClass: 1.154s

Impressive, I didn't know about this!

@kurtextrem
Copy link

If we're talking about perf, we should maybe use the fastest pattern, explicit === undefined: https://esbench.com/bench/620f77c86c89f600a570162f

@mcollina
Copy link
Member

Would you like to send a PR to update this?

@xtx1130
Copy link
Contributor Author

xtx1130 commented Feb 21, 2022

image

The result is almost the same between === undefined and !

@xtx1130
Copy link
Contributor Author

xtx1130 commented Feb 21, 2022

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

=== undefined is more faster at nodejs v16, @kurtextrem would you mind to send a PR to update this?

@kurtextrem
Copy link

At this point we probably shouldn't care or not put any further time into this, see #61

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants