Advertisement
Guest User

Untitled

a guest
Jul 11th, 2013
392
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 IC_filler = 100000000,
  45.         bench = 10000000;
  46.  
  47.     var objs = [{
  48.             prop: 3
  49.         }, {
  50.             prop3: 4,
  51.             prop: 3,
  52.             prop2: 4
  53.         }, {
  54.             prop4: 4,
  55.             prop2: 4,
  56.             prop: 3,
  57.             prop5: 6
  58.         },
  59.         testObject, {
  60.             prop7: 15,
  61.             prop30: 12,
  62.             prop314: 4,
  63.             prop34: 15,
  64.             prop: 3
  65.         }
  66.     ];
  67.     var l = IC_filler;
  68.     while (l--) {
  69.         var index = Math.random() * objs.length | 0;
  70.         megamorphic(objs[index]);
  71.     }
  72.  
  73.     var l = IC_filler;
  74.  
  75.     while (l--) {
  76.         monomorphic(testObject);
  77.     }
  78.  
  79.     var now = Date.now(),
  80.         l = bench;
  81.  
  82.     while (l--) {
  83.         if (megamorphic(testObject) !== 6) {
  84.             die();
  85.         }
  86.     }
  87.  
  88.     print("Megamorphic", Date.now() - now);
  89.  
  90.     var now = Date.now(),
  91.         l = bench;
  92.  
  93.     while (l--) {
  94.         if (monomorphic(testObject) !== 6) {
  95.             die();
  96.         }
  97.     }
  98.  
  99.     print("Monomorphic", Date.now() - now);
  100.  
  101. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement