Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A better solution is to create a temporary constructor function, apply the prototype of the class that you want (to ensure prototype chains are preserved) and then apply the constructor manually. This prevents calling the constructor twice unnecessarily...</p> <pre><code>applySecond = function(){ function tempCtor() {}; return function(ctor, args){ tempCtor.prototype = ctor.prototype; var instance = new tempCtor(); ctor.apply(instance,args); return instance; } }(); </code></pre> <p>I tested the performance and found that this method is, in fact, a bit slower in the <em>very</em> simple case. However, it only takes the construction of a single Date() object in the constructor for this to be more efficient. Also, don't forget that some constructors may throw exceptions if there are no parameters passed, so this is also more correct. </p> <p>My validation code:</p> <pre><code>var ExpensiveClass = function(arg0,arg1){ this.arg0 = arg0; this.arg1 = arg1; this.dat = new Date(); } var CheapClass = function(arg0,arg1){ this.arg0 = arg0; this.arg1 = arg1; } applyFirst = function(ctor, args){ var instance = new ctor(); ctor.apply(instance, args); return instance; } applySecond = function(){ function tempCtor() {}; return function(ctor, args){ tempCtor.prototype = ctor.prototype; var instance = new tempCtor(); ctor.apply(instance,args); return instance; } }(); console.time('first Expensive'); for(var i = 0; i &lt; 10000; i++){ test = applyFirst(ExpensiveClass ,['arg0','arg1']); } console.timeEnd('first Expensive'); console.time('second Expensive'); for(var i = 0; i &lt; 10000; i++){ test = applySecond(ExpensiveClass ,['arg0','arg1']); } console.timeEnd('second Expensive'); console.time('first Cheap'); for(var i = 0; i &lt; 10000; i++){ test = applyFirst(CheapClass,['arg0','arg1']); } console.timeEnd('first Cheap'); console.time('second Cheap'); for(var i = 0; i &lt; 10000; i++){ test = applySecond(CheapClass,['arg0','arg1']); } console.timeEnd('second Cheap'); </code></pre> <p>The results:</p> <pre><code>first Expensive: 76ms second Expensive: 66ms first Cheap: 52ms second Cheap: 52ms </code></pre>
 

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