Guest User

Untitled

a guest
Jul 11th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function benchmark() {
  2.  
  3.     var testObject = {
  4.         prop1: 4,
  5.         prop2: 4,
  6.         prop3: 4,
  7.         prop4: 4,
  8.         prop5: 4,
  9.         prop6: 4,
  10.         prop7: 4,
  11.         prop8: 4,
  12.         prop9: 4,
  13.         prop10: 4,
  14.         prop11: 4,
  15.         prop12: 4,
  16.         prop13: 4,
  17.         prop14: 4,
  18.         prop15: 4,
  19.         prop16: 4,
  20.         prop17: 4,
  21.         prop18: 4,
  22.         prop19: 4,
  23.         prop20: 4,
  24.         prop21: 4,
  25.         prop22: 4,
  26.         prop23: 4,
  27.         prop24: 4,
  28.         prop25: 4,
  29.         prop26: 4,
  30.         prop27: 4,
  31.         prop28: 4,
  32.         prop29: 4,
  33.         prop: 3
  34.     }
  35.  
  36.         function monomorphic(a) {
  37.             return a.prop + a.prop;
  38.         }
  39.  
  40.         function megamorphic(a) {
  41.             return a.prop + a.prop;
  42.         }
  43.  
  44.     var objs = [{
  45.             prop: 3
  46.         }, {
  47.             prop3: 4,
  48.             prop: 3,
  49.             prop2: 4
  50.         }, {
  51.             prop4: 4,
  52.             prop2: 4,
  53.             prop: 3,
  54.             prop5: 6
  55.         },
  56.         testObject, {
  57.             prop7: 15,
  58.             prop30: 12,
  59.             prop314: 4,
  60.             prop34: 15,
  61.             prop: 3
  62.         }
  63.     ];
  64.     var l = 100000000;
  65.     while (l--) {
  66.         var index = Math.random() * objs.length | 0;
  67.         megamorphic(objs[index]);
  68.     }
  69.  
  70.     var l = 100000000;
  71.  
  72.     while (l--) {
  73.         monomorphic(testObject);
  74.     }
  75.  
  76.     var now = Date.now(),
  77.         l = 10000000;
  78.  
  79.     while (l--) {
  80.         if (megamorphic(testObject) !== 6) {
  81.             die();
  82.         }
  83.     }
  84.  
  85.     print("Megamorphic", Date.now() - now);
  86.  
  87.     var now = Date.now(),
  88.         l = 10000000;
  89.  
  90.     while (l--) {
  91.         if (monomorphic(testObject) !== 6) {
  92.             die();
  93.         }
  94.     }
  95.  
  96.     print("Monomorphic", Date.now() - now);
  97.  
  98. })();
Advertisement
Add Comment
Please, Sign In to add comment