为什么检查原型而不是实例?

时间:2022-12-22 05:26:24

I was just reading this answer regarding hashing in Javascript, and, while it was definitely faster than the accepted answer, it requires the reduce function on the Array prototype.

我刚刚在Javascript中阅读有关散列的答案,虽然它肯定比接受的答案快,但它需要在Array原型上使用reduce函数。

Checking the existence of the reduce function is easy enough; but while most checks I do (and have seen) check against the prototype, it just made me wonder: what are the implications of checking against an instance itself? Why does the prototype check seem to be preferred?

检查reduce函数是否存在很容易;但是,虽然我做了(并且已经看过)大多数检查对原型进行检查,但它让我想知道:检查实例本身有什么含义?为什么原型检查似乎更受欢迎?

// i.e.

if (!!Array.prototype.reduce) { }
// vs
if (!![].reduce)

The instance will definitely need an instance, so that's one thing, but is that it?

实例肯定需要一个实例,所以这是一回事,但是它是什么?

2 个解决方案

#1


3  

Just ran a benchmark: http://jsperf.com/prototype-vs-instance-property-check

刚刚运行了一个基准:http://jsperf.com/prototype-vs-instance-property-check

Array.prototype.reduce is 3x faster due to the instantiation of an empty array, but realistically, there is absolutely no difference since these checks are almost always one-time checks, and not in code that runs all the time.

由于空数组的实例化,Array.prototype.reduce的速度提高了3倍,但实际上,由于这些检查几乎总是一次性检查,因此完全没有区别,而不是在一直运行的代码中。

I personally have reduced this to [].method for years. I do the same for things like Array.prototype.slice.call( .. ) vs [].slice.call( ... ), and those are called far more often than once.

我个人已将此减少到[]。方法多年。我对Array.prototype.slice.call(..)vs [] .slice.call(...)之类的东西做了同样的事情,并且这些事件的调用次数远远超过一次。

But note that this is only valid on Array, so you're really not saving a lot.

但请注意,这仅适用于Array,因此您实际上并没有节省太多。

#2


2  

The second version involves needlessly instantiating an empty array. Why do so, when you can just ask the prototype itself and not instantiate anything?

第二个版本涉及不必要地实例化一个空数组。为什么这样做,什么时候你可以问原型本身而不是实例化任何东西?

#1


3  

Just ran a benchmark: http://jsperf.com/prototype-vs-instance-property-check

刚刚运行了一个基准:http://jsperf.com/prototype-vs-instance-property-check

Array.prototype.reduce is 3x faster due to the instantiation of an empty array, but realistically, there is absolutely no difference since these checks are almost always one-time checks, and not in code that runs all the time.

由于空数组的实例化,Array.prototype.reduce的速度提高了3倍,但实际上,由于这些检查几乎总是一次性检查,因此完全没有区别,而不是在一直运行的代码中。

I personally have reduced this to [].method for years. I do the same for things like Array.prototype.slice.call( .. ) vs [].slice.call( ... ), and those are called far more often than once.

我个人已将此减少到[]。方法多年。我对Array.prototype.slice.call(..)vs [] .slice.call(...)之类的东西做了同样的事情,并且这些事件的调用次数远远超过一次。

But note that this is only valid on Array, so you're really not saving a lot.

但请注意,这仅适用于Array,因此您实际上并没有节省太多。

#2


2  

The second version involves needlessly instantiating an empty array. Why do so, when you can just ask the prototype itself and not instantiate anything?

第二个版本涉及不必要地实例化一个空数组。为什么这样做,什么时候你可以问原型本身而不是实例化任何东西?