Skip to content Skip to sidebar Skip to footer

Javascript Benchmarks In Testing Different Emulations Of A "class"

i have read articles that say: using the prototype will be fastest since functions declared are shared. more detail was explained in this article where tapping JS native prototype

Solution 1:

It's not a given that "closures should perform worse". Closures cause each object to get their own copy of the function. But as long as you have enough memory, that shouldn't cause any real performance issues. If anything, closures may even be faster because they don't have to go up the prototype chain to find the property, they are always at the first level right on the object.

The real downside to closures is memory usage, not speed. When creating a ton of objects, it can become a concern.

You also have to consider the runtime in question. Different JavaScript engines will optimize for different situations, depending on what they thought was most important.

Solution 2:

The difference is that closures are looked up on the scope chain, properties on the internal prototype chain. Even though they are both fundamentally object property lookups (one using activation objects, the other plain Objects), it may simply be that the UAs you've tested are optimised more for one than the other.

P.S. In IE 8, prototypes are faster in the example.

Post a Comment for "Javascript Benchmarks In Testing Different Emulations Of A "class""