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"?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With