Performance issues when adding many functions to object prototypes in Javascript?

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 As for why there is no speed hit: In order to resolve a function call on an object: foo.doSomething() the internal engine needs to walk the object scope and prototype chain of that object. Basically, it does this: if(Object. HasOwnProperty('doSomething')){ // run Object.

DoSomething if(Object. __proto__. HasOwnProperty('doSomething')){ // run Object.

__proto__. DoSomething while(Object. __proto__.

__proto__. __proto__.....){ // repeat Every layer of this is a hash, so lookups are constant time In terms of lookup speed 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) For reference: jQuery has 511 functions internally. 200 isn't that many at all Side note: DO NOT EXTEND Object.

Prototype 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 if(obj. HasOwnProperty(foo)) 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 And PLEASE don't extend Array. Prototype it annoys me. But plenty of other people do it, so it's not -as- bad... The argument is that you're not supposed 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!

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. As for why there is no speed hit: In order to resolve a function call on an object: foo.doSomething(), the internal engine needs to walk the object scope and prototype chain of that object. Basically, it does this: if(Object.

HasOwnProperty('doSomething')){ // run Object. DoSomething if(Object. __proto__.

HasOwnProperty('doSomething')){ // run Object. __proto__. DoSomething while(Object.

__proto__. __proto__. __proto__.....){ // repeat Every layer of this is a hash, so lookups are constant time.In terms of lookup speed, 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).

For reference: jQuery has 511 functions internally. 200 isn't that many at all Side note: DO NOT EXTEND Object. Prototype -- 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 if(obj. HasOwnProperty(foo)) 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.

And PLEASE don't extend Array. Prototype -- it annoys me. But plenty of other people do it, so it's not -as- bad... The argument is that you're not supposed 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!

Hundreds' seems like a lot. But suit yourself, if you can afford to. If you work alone, and don't expect that to change, you can modify the native prototypes all you like.

Otherwise you really have to be the boss of your outfit to get away with it. No matter how clever an innovation, getting other people to use it is the hard part. It doesn't take longer or use more resources to run the same method from inside than outside an object.

It doesn't take longer ... to run the same method from inside than outside an object. " that's not entirely true. Scope lookups take different amounts of time depending on things like how many scope-swaps need to occur to find an object.In Chrome, Object.

Prototype lookups are actually among the slowest lookups there are, but even so a quick test shows that it only takes 100ms to do 1 million lookups against an object (conversely it takes 4ms to do 1 million lookups against a function in-scope). That's not to say those are the results in every browser, however. – cwolves Apr 7 at 1:12.

Yes there will be quite a performance toll. Generally it's considered bad practice to extend (too much) the basic objects. An example of a library that could have used that path is Underscore.

Js - but instead they have their own object _ which they use to wrap stuff up. That makes it faster. A library that did add stuff to basic objects is Fuctional.

Js - and I believe that's one of the reasons it's not commonly used. The reason why this is bad is that in every JavaScript function call the complete prototype chain must be searched to resolve a function call - so if you extend object, every object you create will have a much larger space to search. Also if someone uses a for in loop this will obviously take much longer.

No, there won't be. That's the entire point of prototypical inheritance -- that there's almost no hit on stuff like this. There's no extra scope lookup hit since the prototype is hashed anyway (every prototype does NOT need to be 'searched'), and 200 functions isn't many at all (I have apps with 2,000 running).

And Underscore. Js having their own object has absolutely no impact on the speed of anything. – cwolves Apr 7 at 0:45.

The issues around JS and inheritance may be complex, but the answer to your question is relatively simple. Now, if you add a property to obj1, that property exists only on obj1. If you add a property to Klass, that property likewise exists only on Klass (the function object).

It doesn't affect obj1 and obj2 at all. But if you add a property to Klass. Prototype, that property will then be present on both obj1 and obj2, as well as any future objects created via 'new Klass'.

If you then change the value of the property on the prototype, the changed value will be what you see on all those objects. You could add code inside the body of the Klass function to add properties to this; that will then cause any future Klass objects to get those properties. But each object would have its own copy - which can add up, memory-wise, especially when the properties are methods -and those copies would not be affected by future changes to the body of Klass.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions