使用instanceof运算符在JavaScript性能问题? [英] Is using instanceof operator in javascript a performance issue?

查看:219
本文介绍了使用instanceof运算符在JavaScript性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在java中的instanceof运算符是一个性能问题,

I have heard that instanceof operator in java is a performance issue,

这是真的为Javascript太(IE6,IE7,IE8,FF,铬,野生动物园等)?
以真实的文书的任何链接将是有益的。

Is it true for Javascript too (IE6,IE7,IE8,FF,Chrome,safari,etc.)? any links to authentic papers would be helpful.

推荐答案

在短期:这似乎是依赖于浏览器

In short: it seems to be browser dependent.

更详细:
我发现这JSPerf测试: http://jsperf.com/instanceof-performance/2 比较一个JavaScript的instanceof检查与一个布尔检查中的对象的现有/缺少的属性。

More detailed: I have found this JSPerf test: http://jsperf.com/instanceof-performance/2 comparing a JavaScript instanceof check versus a boolean check for an existing/missing property in an object.

总的结果(提防小样本)的是,在Chrome中这两种方法都是一样的与效益的instanceof。在FF,然而,属性检查比instanceof运算更快。

The overall result (beware of the small number of samples) is that in Chrome both methods are alike with benefits for instanceof. In FF, however, the property check is faster than the instanceof operator.

将是有趣的是测试用的情况下延长象检查,如果一个字符串比较像 obj.type =='MyClass的'对这个问题有很大的影响。

Would be interesting to extend that test with a case like checking if a string comparison like obj.type == 'MyClass' has a strong influence on the subject.

这篇关于使用instanceof运算符在JavaScript性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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