I have heard that instanceof operator in java is a performance issue,
Is it true for Javascript too (IE6,IE7,IE8,FF,Chrome,safari,etc.)? any links to authentic papers would be helpful.
I have heard that instanceof operator in java is a performance issue,
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.
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.
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. Update Apr 2017: As @ngryman pointed out: In both, recent FF and Chrome versions, doing property checks seems significantly faster than instenaceof.
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.
You could pretty easily make your own JavaScript benchmark similar to this one linked from Kaleb's link.
That's not true for Java anymore -- see here.
As for Javascript, I couldn't find any articles that discuss this, but I highly doubt that instanceof would be cause any performance issues. If you need to use it, I would say go for it, and then reconsider only if you're running into performance problems.
I wouldn't worry about performance of the instanceof operator myself, because JavaScript itself is rarely a reason of performance problems. DOM manipulations usually take much more time. However, if you need instanceof in a heavy used loop, I would suggest to profile it using FireBug profiler.