15

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.

Salvin Francis
  • 4,117
  • 5
  • 35
  • 43
  • 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 Jan 05 '10 at 20:15
  • Hmm, my question wasn't framed in the right way. – Salvin Francis Jan 06 '10 at 04:40
  • 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 Jan 06 '10 at 20:32
  • This should be a comment, not an answer. OP was clearly asking for links to papers/data on the performance of `instanceof`, which is not vague. – brokethebuildagain Jan 06 '15 at 21:36
  • If you want to skip the bloat, here's a JSPerf : http://jsperf.com/instanceof-performance/2 –  Mar 30 '18 at 15:13

4 Answers4

19

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.

Christopher Lörken
  • 2,740
  • 18
  • 17
0

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

Annie
  • 6,621
  • 22
  • 27
-1

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.

Community
  • 1
  • 1
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
  • 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 Dec 30 '09 at 06:40
-2

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.

  • 2
    if you assume it's frontend JavaScript. Supposing it's Node.js... – josiah Jan 27 '15 at 01:29
  • 3
    "JavaScript itself is rarely a reason of performance problems" -- Maybe it was in 2009 :) but in 2020, with heavy SPAs and lots of data updated/loaded interactively, and with the fact that DOM manipulations is not so obvious and hidden within frameworks, 'not worrying' about performance of something is the same as telling yourself 'prepare to [un]foreseen consequences' – Andrii M4n0w4R May 22 '20 at 09:49