【问题标题】:Performance: typeof vs instanceof性能:typeof vs instanceof
【发布时间】:2018-10-13 22:00:51
【问题描述】:

我想知道typeofinstanceof 哪个性能更高,所以我把以下小东西放在一起:

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'")

并从 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

Firefox 59 运行该 XD 需要很长时间

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

let TIMES = 1000 * 1000 * 10

我从 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

两个结果集都显示typeofinstanceof 快得多,Which is best to use: typeof or instanceof? 中也提到了其他几个人。

我的问题是“y tho”?

【问题讨论】:

标签: javascript performance instanceof typeof


【解决方案1】:

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

【讨论】:

  • 在这种情况下,(() =&gt; {}).__proto__ 直接是 Function.prototype,所以这只是“一次检查”
  • 对于箭头功能是的。考虑到 proto 类似于 Object.getPrototypeOf,似乎仍然如此,typeof 在幕后更快,无论做什么,都比对象属性访问便宜。
  • 这只是一个关于使用一个班轮性能测试的一般提醒,而没有过多关注细节(优化是主要因素,cpu 时钟精度,......) - nice performance。这就是为什么我对 stackoverflow 上 95% 以上的“性能测试”投反对票的原因,这些测试盲目地从拙劣的测试中得出结论。我并不是说结果是错误的,只是奇怪的 jsperf 测试不应该是论证的基础。
  • 不,不应该,我同意。有大量的瓶颈需要优化,直到它归结为不可填充的内置规范之间的对比。我也不认为这可以公平比较,因为这两个操作数根本不一样。
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 2011-02-25
  • 2011-08-28
  • 2010-09-16
  • 1970-01-01
  • 2014-12-15
  • 2016-12-18
相关资源
最近更新 更多