Advertisement
marcelsmudda

TemplateString v2

Oct 5th, 2017
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ITERATIONS = 100000000;
  2. const RUNS = 100;
  3.  
  4. const templateStartString = function () {
  5.   const before = new Date().getTime();
  6.   let l = 0;
  7.   for (let i = 0; i < ITERATIONS; i++) {
  8.     l += `${i}def`.length;
  9.   }
  10.   const after = new Date().getTime();
  11.   console.log(l);
  12.   console.log(after-before);
  13.   return after-before;
  14. }
  15.  
  16. const templateMidString = function () {
  17.   const before = new Date().getTime();
  18.   let l = 0;
  19.   for (let i = 0; i < ITERATIONS; i++) {
  20.     l += `abc${i}def`.length;
  21.   }
  22.   const after = new Date().getTime();
  23.   console.log(l);
  24.   console.log(after-before);
  25.   return after-before;
  26. }
  27.  
  28. const templateEndString = function () {
  29.   const before = new Date().getTime();
  30.   let l = 0;
  31.   for (let i = 0; i < ITERATIONS; i++) {
  32.     l += `abc${i}`.length;
  33.   }
  34.   const after = new Date().getTime();
  35.   console.log(l);
  36.   console.log(after-before);
  37.   return after-before;
  38. }
  39.  
  40. const templateDoubleString = function () {
  41.   const before = new Date().getTime();
  42.   let l = 0;
  43.   for (let i = 0; i < ITERATIONS; i++) {
  44.     l += `abc${i}def${i}ghi`.length;
  45.   }
  46.   const after = new Date().getTime();
  47.   console.log(l);
  48.   console.log(after-before);
  49.   return after-before;
  50. }
  51.  
  52. const templateManyString = function () {
  53.   const before = new Date().getTime();
  54.   let l = 0;
  55.   for (let i = 0; i < ITERATIONS; i++) {
  56.     l += `${i}abc${i}def${i}ghi${i}jkl${i}mno${i}pqr${i}stu${i}vwx${i}yz${i}`.length;
  57.   }
  58.   const after = new Date().getTime();
  59.   console.log(l);
  60.   console.log(after-before);
  61.   return after-before;
  62. }
  63.  
  64. const concatStartString = function () {
  65.   const before = new Date().getTime();
  66.   let l = 0;
  67.   for (let i = 0; i < ITERATIONS; i++) {
  68.     l += (i + 'def').length;
  69.   }
  70.   const after = new Date().getTime();
  71.   console.log(l);
  72.   console.log(after-before);
  73.   return after-before;
  74. }
  75.  
  76. const concatMidString = function () {
  77.   const before = new Date().getTime();
  78.   let l = 0;
  79.   for (let i = 0; i < ITERATIONS; i++) {
  80.     l += ('abc' + i + 'def').length;
  81.   }
  82.   const after = new Date().getTime();
  83.   console.log(l);
  84.   console.log(after-before);
  85.   return after-before;
  86. }
  87.  
  88. const concatEndString = function () {
  89.   const before = new Date().getTime();
  90.   let l = 0;
  91.   for (let i = 0; i < ITERATIONS; i++) {
  92.     l += ('abc' + i).length;
  93.   }
  94.   const after = new Date().getTime();
  95.   console.log(l);
  96.   console.log(after-before);
  97.   return after-before;
  98. }
  99.  
  100. const concatDoubleString = function () {
  101.   const before = new Date().getTime();
  102.   let l = 0;
  103.   for (let i = 0; i < ITERATIONS; i++) {
  104.     l += ('abc' + i + 'def' + i + 'ghi').length;
  105.   }
  106.   const after = new Date().getTime();
  107.   console.log(l);
  108.   console.log(after-before);
  109.   return after-before;
  110. }
  111.  
  112. const concatManyString = function () {
  113.   const before = new Date().getTime();
  114.   let l = 0;
  115.   for (let i = 0; i < ITERATIONS; i++) {
  116.     l += (i + 'abc' + i + 'def' + i + 'ghi' + i + 'jkl' + i + 'mno' + i + 'pqr' + i + 'stu' + i + 'vwx' + i + 'yz' + i).length;
  117.   }
  118.   const after = new Date().getTime();
  119.   console.log(l);
  120.   console.log(after-before);
  121.   return after-before;
  122. }
  123.  
  124. const o = {
  125.   t: {
  126.     start: [],
  127.     mid: [],
  128.     end: [],
  129.     double: [],
  130.     many: []
  131.   },
  132.   c: {
  133.     start: [],
  134.     mid: [],
  135.     end: [],
  136.     double: [],
  137.     many: []
  138.   }
  139. }
  140.  
  141. // warm up
  142. console.log('start warm up');
  143. concatStartString();
  144. console.log('warm up done');
  145.  
  146. for (let i = 0; i < RUNS; i++) {
  147.   console.log('RUN ' + ('000' + (i+1)).slice(-3));
  148.   o.t.start.push(templateStartString());
  149.   o.t.mid.push(templateMidString());
  150.   o.t.end.push(templateEndString());
  151.   o.t.double.push(templateDoubleString());
  152.   o.t.many.push(templateManyString());
  153.   o.c.start.push(concatStartString());
  154.   o.c.mid.push(concatMidString());
  155.   o.c.end.push(concatEndString());
  156.   o.c.double.push(concatDoubleString());
  157.   o.c.many.push(concatManyString());
  158. }
  159. const calcs = {
  160.   sum: {
  161.     t: {
  162.       start: 0,
  163.       mid: 0,
  164.       end: 0,
  165.       double: 0,
  166.       many: 0
  167.     },
  168.     c: {
  169.       start: 0,
  170.       mid: 0,
  171.       end: 0,
  172.       double: 0,
  173.       many: 0
  174.     }
  175.   },
  176.   avg: {
  177.     t: {
  178.       start: 0,
  179.       mid: 0,
  180.       end: 0,
  181.       double: 0,
  182.       many: 0
  183.     },
  184.     c: {
  185.       start: 0,
  186.       mid: 0,
  187.       end: 0,
  188.       double: 0,
  189.       many: 0
  190.     }
  191.   },
  192.   sd: {
  193.     t: {
  194.       start: 0,
  195.       mid: 0,
  196.       end: 0,
  197.       double: 0,
  198.       many: 0
  199.     },
  200.     c: {
  201.       start: 0,
  202.       mid: 0,
  203.       end: 0,
  204.       double: 0,
  205.       many: 0
  206.     }
  207.   },
  208.   aad: {
  209.     t: {
  210.       start: 0,
  211.       mid: 0,
  212.       end: 0,
  213.       double: 0,
  214.       many: 0
  215.     },
  216.     c: {
  217.       start: 0,
  218.       mid: 0,
  219.       end: 0,
  220.       double: 0,
  221.       many: 0
  222.     }
  223.   },
  224.   min: {
  225.     t: {
  226.       start: 0,
  227.       mid: 0,
  228.       end: 0,
  229.       double: 0,
  230.       many: 0
  231.     },
  232.     c: {
  233.       start: 0,
  234.       mid: 0,
  235.       end: 0,
  236.       double: 0,
  237.       many: 0
  238.     }
  239.   },
  240.   max: {
  241.     t: {
  242.       start: 0,
  243.       mid: 0,
  244.       end: 0,
  245.       double: 0,
  246.       many: 0
  247.     },
  248.     c: {
  249.       start: 0,
  250.       mid: 0,
  251.       end: 0,
  252.       double: 0,
  253.       many: 0
  254.     }
  255.   },
  256.   median: {
  257.     t: {
  258.       start: 0,
  259.       mid: 0,
  260.       end: 0,
  261.       double: 0,
  262.       many: 0
  263.     },
  264.     c: {
  265.       start: 0,
  266.       mid: 0,
  267.       end: 0,
  268.       double: 0,
  269.       many: 0
  270.     }
  271.   }
  272. }
  273.  
  274. const calcTemplate = function (name) {
  275.   for (const v of o.t[name]) {
  276.     calcs.sum.t[name] += v;
  277.   }
  278.   calcs.avg.t[name] = calcs.sum.t[name] / RUNS;
  279.   for (const value of o.t[name]) {
  280.     const v = value - calcs.avg.t[name];
  281.     calcs.sd.t[name] += v*v;
  282.     calcs.aad.t[name] += Math.abs(v);
  283.   }
  284.   calcs.sd.t[name] = calcs.sd.t[name] / RUNS;
  285.   calcs.aad.t[name] = calcs.aad.t[name] / RUNS;
  286.   calcs.sd.t[name] = Math.pow(calcs.sd.t[name], 0.5);
  287.   calcs.min.t[name] = Math.min(...o.t[name]);
  288.   calcs.max.t[name] = Math.max(...o.t[name]);
  289.   o.t[name].sort((a,b) => a-b);
  290.   calcs.median.t[name] = (o.t[name][(o.t[name].length - 1) >> 1] + o.t[name][o.t[name].length >> 1]) / 2;
  291. }
  292.  
  293. const calcConcat = function (name) {
  294.   for (const v of o.c[name]) {
  295.     calcs.sum.c[name] += v;
  296.   }
  297.   calcs.avg.c[name] = calcs.sum.c[name] / RUNS;
  298.   for (const value of o.c[name]) {
  299.     const v = value - calcs.avg.c[name];
  300.     calcs.sd.c[name] += v*v;
  301.     calcs.aad.c[name] += Math.abs(v);
  302.   }
  303.   calcs.sd.c[name] = calcs.sd.c[name] / RUNS;
  304.   calcs.aad.c[name] = calcs.aad.c[name] / RUNS;
  305.   calcs.sd.c[name] = Math.pow(calcs.sd.c[name], 0.5);
  306.   calcs.min.c[name] = Math.min(...o.c[name]);
  307.   calcs.max.c[name] = Math.max(...o.c[name]);
  308.   calcs.median.c[name] = (o.c[name][(o.c[name].length - 1) >> 1] + o.c[name][o.c[name].length >> 1]) / 2;
  309. }
  310.  
  311. const calc = function () {
  312.   calcTemplate('start');
  313.   calcTemplate('mid');
  314.   calcTemplate('end');
  315.   calcTemplate('double');
  316.   calcTemplate('many');
  317.  
  318.   calcConcat('start');
  319.   calcConcat('mid');
  320.   calcConcat('end');
  321.   calcConcat('double');
  322.   calcConcat('many');
  323. }
  324.  
  325. calc();
  326. console.log(JSON.stringify(calcs, null, 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement