性能:typeof 与 instanceof

Val*_*one 5 javascript performance typeof instanceof

我想知道哪个typeof和哪个instanceof更高效,所以我把以下小东西放在一起:

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'")
Run Code Online (Sandbox Code Playgroud)

并从 Chrome 66 的控制台获得了这些超酷的结果:

(() => { }) 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
Run Code Online (Sandbox Code Playgroud)

Firefox 59 花了很长时间来运行 XD

我没有足够的耐心等待它并且使 TIMES 减少了十倍:

let TIMES = 1000 * 1000 * 10
Run Code Online (Sandbox Code Playgroud)

有了这个,我从 Firefox 59 的控制台中得到了以下内容:

(() => { }) 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
Run Code Online (Sandbox Code Playgroud)

两个结果集都显示typeof比 快得多instanceof,事实上其他几个人也在哪个最好使用:typeof 或 instanceof?.

我的问题是“y tho”?

ibr*_*cin 2

正如您还预测并显示了来自 SO 的其他链接,typeof速度会更快(jsperf: https: //jsperf.com/instanceof-vs-typeof/5),这是有道理的,因为typeof将返回内置类型之一(对象, undefined 和原语),同时您可以控制操作数的右侧instanceof。这意味着instanceof操作员应该检查原型链并查看右侧是否已在某处用作构造函数。这自然是工作量更大了。