Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. var Obj = function(a){ this.a = a;};
  2. Obj.prototype.test = function(b){ return ++b; };
  3.  
  4. var Obj2 = function(){
  5.  
  6. //actual function returned by call
  7. function MyConstructor( a ){ this.a = a; }
  8.  
  9. //a function we'll use on this object
  10. function test(b){ return ++b; }
  11.  
  12. //get the prototypa reference
  13. var _p = MyConstructor.prototype;
  14.  
  15. //assign as constructor
  16. _p.constructor = MyConstructor;
  17.  
  18. //assign the function to the prototype
  19. MyConstructor.prototype.test = test;
  20.  
  21. //now Obj2 = Myconstructor
  22. return MyConstructor;
  23. }();// <- call
  24.  
  25. function main(){
  26.  
  27. var i, o, v, nb = 10000000;
  28.  
  29. console.time( "obj1" );
  30. v = 0;
  31. for(i=0;i<nb;i++)
  32. {
  33. o = new Obj( "a" );
  34. v = o.test(v);
  35. }
  36. console.timeEnd( "obj1" );
  37. console.log( "v = ", v );
  38.  
  39. console.time( "obj2" );
  40. v = 0;
  41. for(i=0;i<nb;i++)
  42. {
  43. o = new Obj2( "a" );
  44. v = o.test(v);
  45. }
  46. console.timeEnd( "obj2" );
  47. console.log( "v = ", v );
  48.  
  49. }
  50. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement