views:

285

answers:

5

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.

+1  A: 

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.

Kaleb Brasee
even i didn't find any articles that say that, but i guess, there could be an impact. Still waiting for a conclusion/benchmark result...
Salvin Francis
+1  A: 

You could pretty easily make your own JavaScript benchmark similar to this one linked from Kaleb's link.

Annie
hmm. I am doing my own tests too, but i would still prefer to refer an article or paper on this issue.
Salvin Francis
+1  A: 

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.

valums
A: 

Pre-mature optimization is the root cause of all evil. Try it out and if you run into any performance issues, then try to solve the problem.

Don't try to solve something which is never a problem in first place for you.

Ramesh
I really do honor the comments here, but, I see most of you guys are not that keen in answering the question.I am asking this from knowledge point of view not a project point of view. Maybe I haven't framed my question right.
Salvin Francis
+1  A: 

You're looking for something definitive, I see. But "performance issue" isn't a definitive question. On the contrary, it's very vague.

Also, performance questions need to be asked in the form "Does A or B require more memory/run time/other measurable quantity", where A and B are different things which produce the same result. So you would need to compare the instanceof operator to something else which does the same thing.

Paul Clapham
Hmm, my question wasn't framed in the right way.
Salvin Francis
I suppose you might run a benchmark which told you that the instanceof operator took 7 microseconds (number chosen randomly just for example) to execute. But then you would have to ask yourself what that meant in the context of some actual code.
Paul Clapham