Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: typeof vs instanceof

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"?

like image 645
Valentine Stone Avatar asked Nov 08 '22 07:11

Valentine Stone


1 Answers

As you also predicted and showed other links from SO, typeof will be faster (jsperf : https://jsperf.com/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.

like image 105
ibrahim tanyalcin Avatar answered Nov 14 '22 21:11

ibrahim tanyalcin