为什么Javascript的“在...中”运算符始终比严格的成员比较慢到未定义? [英] Why is Javascript's "in" operator consistently slower than strict member comparison to undefined?

查看:71
本文介绍了为什么Javascript的“在...中”运算符始终比严格的成员比较慢到未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅 http://jsperf.com/in-vs-member-object-access

基本上,为什么检查 if('bar'在foo中){} 明显慢于 if(foo.bar!== undefined){}

Essentially, why is checking if ('bar' in foo) {} significantly slower than if (foo.bar !== undefined) {}?

推荐答案

foo.bar!== undefined 只检查这两个值是否匹配。

foo.bar !== undefined checks just those 2 values to see if they match.

虽然' bar'in foo 将不得不使用某种机制来遍历 foo 的属性,看看 bar 就在其中。

While 'bar' in foo will have to use some mechanism to loop through the properties of foo to see if bar is in it.

这是一个有趣的从Ecma脚本中读取

Here is an interesting Read from Ecma-script


in运算符



ShiftExpression中的生产RelationalExpression:RelationalExpression的计算方法如下:

1.评估RelationalExpression。

2.调用GetValue(结果(1))。

3.评估ShiftExpression。

4.调用GetValue(结果(3))。

5。如果Result(4)不是对象,则抛出TypeError异常。

6.调用ToString(Result(2))。

7.调用[[HasProperty]]方法结果(4)的结果(4)。

8.返回结果(7)。

The in operator

The production RelationalExpression : RelationalExpression in ShiftExpression is evaluated as follows:
1. Evaluate RelationalExpression.
2. Call GetValue(Result(1)).
3. Evaluate ShiftExpression.
4. Call GetValue(Result(3)).
5. If Result(4) is not an object, throw a TypeError exception.
6. Call ToString(Result(2)).
7. Call the [[HasProperty]] method of Result(4) with parameter Result(6).
8. Return Result(7).

生产EqualityExpression:EqualityExpression!== RelationalExpression的计算方法如下:

1.评估EqualityExpression。

2.调用GetValue(Result(1))。

3.评估RelationalExpression。

4.调用GetValue(Result(3))。

5.执行比较结果(4)===结果(2)。 (见下文。)

6.如果Result(5)为true,则返回false。否则,返回true。

The production EqualityExpression : EqualityExpression !== RelationalExpression is evaluated as follows:
1. Evaluate EqualityExpression.
2. Call GetValue(Result(1)).
3. Evaluate RelationalExpression.
4. Call GetValue(Result(3)).
5. Perform the comparison Result(4) === Result(2). (See below.)
6. If Result(5) is true, return false. Otherwise, return true.

这篇关于为什么Javascript的“在...中”运算符始终比严格的成员比较慢到未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆