javascript - Performance: typeof vs instanceof - Stack Overflow

I was wondering which one of typeof and instanceof is more performant, so I threw together the followin

I was wondering which one of typeof and instanceof is more performant, so I threw together the following little thing:

let TIMES = 1000 * 1000 * 100

console.time("(() => { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (() => { }) instanceof Function
console.timeEnd("(() => { }) instanceof Function")

console.time("(async () => { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (async () => { }) instanceof Function
console.timeEnd("(async () => { }) instanceof Function")

console.time("(function () { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (function () { }) instanceof Function
console.timeEnd("(function () { }) instanceof Function")

console.time("(async function () { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (async function () { }) instanceof Function
console.timeEnd("(async function () { }) instanceof Function")

console.time("typeof (() => { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (() => { }) === 'function'
console.timeEnd("typeof (() => { }) === 'function'")

console.time("typeof (async () => { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (async () => { }) === 'function'
console.timeEnd("typeof (async () => { }) === 'function'")

console.time("typeof (function () { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (function () { }) === 'function'
console.timeEnd("typeof (function () { }) === 'function'")

console.time("typeof (async function () { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (async function () { }) === 'function'
console.timeEnd("typeof (async function () { }) === 'function'")

And got these super cool results from Chrome 66's console:

(() => { }) instanceof Function: 1789.844970703125ms
(async () => { }) instanceof Function: 2229.64208984375ms
(function () { }) instanceof Function: 1954.09716796875ms
(async function () { }) instanceof Function: 2279.995849609375ms
typeof (() => { }) === 'function': 412.8701171875ms
typeof (async () => { }) === 'function': 413.337890625ms
typeof (function () { }) === 'function': 413.387939453125ms
typeof (async function () { }) === 'function': 412.910888671875ms

Firefox 59 took forever to run that XD

I didn't have enough patience to wait for it and made TIMES ten times less:

let TIMES = 1000 * 1000 * 10

With that I got the following out of Firefox 59's console:

(() => { }) instanceof Function: 5490ms
(async () => { }) instanceof Function: 6884ms
(function () { }) instanceof Function: 5408ms
(async function () { }) instanceof Function: 6938ms
typeof (() => { }) === 'function': 1916ms
typeof (async () => { }) === 'function': 1998ms
typeof (function () { }) === 'function': 1976ms
typeof (async function () { }) === 'function': 1972ms

Both result sets show that typeof is much faster than instanceof, the fact a couple of other people also mentioned in Which is best to use: typeof or instanceof?.

My question is "y tho"?

I was wondering which one of typeof and instanceof is more performant, so I threw together the following little thing:

let TIMES = 1000 * 1000 * 100

console.time("(() => { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (() => { }) instanceof Function
console.timeEnd("(() => { }) instanceof Function")

console.time("(async () => { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (async () => { }) instanceof Function
console.timeEnd("(async () => { }) instanceof Function")

console.time("(function () { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (function () { }) instanceof Function
console.timeEnd("(function () { }) instanceof Function")

console.time("(async function () { }) instanceof Function")
for (let i = 0; i < TIMES; i++)
  (async function () { }) instanceof Function
console.timeEnd("(async function () { }) instanceof Function")

console.time("typeof (() => { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (() => { }) === 'function'
console.timeEnd("typeof (() => { }) === 'function'")

console.time("typeof (async () => { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (async () => { }) === 'function'
console.timeEnd("typeof (async () => { }) === 'function'")

console.time("typeof (function () { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (function () { }) === 'function'
console.timeEnd("typeof (function () { }) === 'function'")

console.time("typeof (async function () { }) === 'function'")
for (let i = 0; i < TIMES; i++)
  typeof (async function () { }) === 'function'
console.timeEnd("typeof (async function () { }) === 'function'")

And got these super cool results from Chrome 66's console:

(() => { }) instanceof Function: 1789.844970703125ms
(async () => { }) instanceof Function: 2229.64208984375ms
(function () { }) instanceof Function: 1954.09716796875ms
(async function () { }) instanceof Function: 2279.995849609375ms
typeof (() => { }) === 'function': 412.8701171875ms
typeof (async () => { }) === 'function': 413.337890625ms
typeof (function () { }) === 'function': 413.387939453125ms
typeof (async function () { }) === 'function': 412.910888671875ms

Firefox 59 took forever to run that XD

I didn't have enough patience to wait for it and made TIMES ten times less:

let TIMES = 1000 * 1000 * 10

With that I got the following out of Firefox 59's console:

(() => { }) instanceof Function: 5490ms
(async () => { }) instanceof Function: 6884ms
(function () { }) instanceof Function: 5408ms
(async function () { }) instanceof Function: 6938ms
typeof (() => { }) === 'function': 1916ms
typeof (async () => { }) === 'function': 1998ms
typeof (function () { }) === 'function': 1976ms
typeof (async function () { }) === 'function': 1972ms

Both result sets show that typeof is much faster than instanceof, the fact a couple of other people also mentioned in Which is best to use: typeof or instanceof?.

My question is "y tho"?

Share Improve this question asked May 3, 2018 at 14:31 Valentine StoneValentine Stone 1,2051 gold badge9 silver badges7 bronze badges 4
  • 4 But both are not interchangeable. The usecases of both the operators vary in certain cases. See this post. – 31piy Commented May 3, 2018 at 14:33
  • 1 typeof checks only the type (one simple check), instanceof check also if it belongs to a super class (multiple checks) – Naramsim Commented May 3, 2018 at 14:35
  • 1 My guess is because variables in the VM have a type token associated with them by default, which is quicker to check than pointer-chasing up to the prototype chain. – Máté Safranka Commented May 3, 2018 at 14:35
  • 3 The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming. – Liam Commented May 3, 2018 at 14:47
Add a ment  | 

1 Answer 1

Reset to default 2

As you also predicted and showed other links from SO, typeof will be faster (jsperf : https://jsperf./instanceof-vs-typeof/5), and this makes sense because typeof will return one of the built in types (Object, undefined and the primitives) while you get to control the right side of the instanceof operand. This means that the instanceof operator should check the prototype chain and see if the right side has been used as a constructor somewhere along. This is naturally more work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316073a4622225.html

相关推荐

  • javascript - Performance: typeof vs instanceof - Stack Overflow

    I was wondering which one of typeof and instanceof is more performant, so I threw together the followin

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信