Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, there will be almost no performance hit. Even attaching the functions to the prototype(s) should only take a few ms (maybe 5ms) and beyond that you have 200 functions sitting in memory in one spot, never being copied, you will never notice anything. Assuming, of course, that you are attaching to .prototype of something.</p> <p>As for why there is no speed hit: In order to resolve a function call on an object: <code>foo.doSomething()</code>, the internal engine needs to walk the object scope and prototype chain of that object. Basically, it does this:</p> <ul> <li><code>if(Object.hasOwnProperty('doSomething')){ // run Object.doSomething</code></li> <li><code>if(Object.__proto__.hasOwnProperty('doSomething')){ // run Object.__proto__.doSomething</code></li> <li><code>while(Object.__proto__.__proto__.__proto__.....){ // repeat</code></li> </ul> <p><em>Every</em> layer of this is a hash, so lookups are constant time. <em>In terms of lookup speed</em>, it doesn't matter if there's 2 or 2 million functions in a prototype chain (Although if you have 2 million you'll be eating tons of memory).</p> <p>For reference: jQuery has 511 functions internally. 200 isn't that many at all</p> <p>Side note: DO NOT EXTEND <code>Object.prototype</code> -- JUST DON'T. You break for-in loops if you do this, or at least come very close to breaking them if people don't use explicit <code>if(obj.hasOwnProperty(foo))</code> checks. You'll also make for-in loops slower on object hashes which is the ONLY potential slow-down you'll ever encounter extending prototypes.</p> <p>And PLEASE don't extend <code>Array.prototype</code> -- it annoys me. But plenty of other people do it, so it's not -as- bad... The argument is that you're not <em>supposed</em> to use for-in loops on Arrays, and many people now don't because of Prototype.js, but you should still be allowed to if you want to!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload