To prototype or not to prototype?

I am using jsperf a lot lately. Here is another interesting result:

Prototype-based object creation is lightning fast, compared to returning plain objects from simple functions. (see my test on jsperf)



But using prototype-based stuff usually forces you to access properties via this
which is an additional lookup in the prototype chain, that will add some overhead when accessing these object a lot. (see my other test on jsperf)



For you as a JavaScript developer, this means you have to consider the following.

  • Is my code creating a lot of (similar) objects? Then you should setup some reusable types by using prototypes.
  • Is my code accessing properties of some of my objects a lot? Then you should get rid of the access via this and add some for form of property caching in local variables.
Ciao,
Juve

Comments