Advertisement
marcelsmudda

TemplateString.js

Oct 1st, 2017
510
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 concatStartString = function () {
  53.   const before = new Date().getTime();
  54.   let l = 0;
  55.   for (let i = 0; i < ITERATIONS; i++) {
  56.     l += (i + 'def').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 concatMidString = function () {
  65.   const before = new Date().getTime();
  66.   let l = 0;
  67.   for (let i = 0; i < ITERATIONS; i++) {
  68.     l += ('abc' + 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 concatEndString = function () {
  77.   const before = new Date().getTime();
  78.   let l = 0;
  79.   for (let i = 0; i < ITERATIONS; i++) {
  80.     l += ('abc' + i).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 concatDoubleString = function () {
  89.   const before = new Date().getTime();
  90.   let l = 0;
  91.   for (let i = 0; i < ITERATIONS; i++) {
  92.     l += ('abc' + i + 'def' + i + 'ghi').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 o = {
  101.   t: {
  102.     start: [],
  103.     mid: [],
  104.     end: [],
  105.     double: []
  106.   },
  107.   c: {
  108.     start: [],
  109.     mid: [],
  110.     end: [],
  111.     double: []
  112.   }
  113. }
  114.  
  115. for (let i = 0; i < RUNS; i++) {
  116.   console.log(`RUN ${i+1}`);
  117.   o.t.start.push(templateStartString());
  118.   o.t.mid.push(templateMidString());
  119.   o.t.end.push(templateEndString());
  120.   o.t.double.push(templateDoubleString());
  121.   o.c.start.push(concatStartString());
  122.   o.c.mid.push(concatMidString());
  123.   o.c.end.push(concatEndString());
  124.   o.c.double.push(concatDoubleString());
  125. }
  126. const calcs = {
  127.   sum: {
  128.     t: {
  129.       start: 0,
  130.       mid: 0,
  131.       end: 0,
  132.       double: 0
  133.     },
  134.     c: {
  135.       start: 0,
  136.       mid: 0,
  137.       end: 0,
  138.       double: 0
  139.     }
  140.   },
  141.   avg: {
  142.     t: {
  143.       start: 0,
  144.       mid: 0,
  145.       end: 0,
  146.       double: 0
  147.     },
  148.     c: {
  149.       start: 0,
  150.       mid: 0,
  151.       end: 0,
  152.       double: 0
  153.     }
  154.   },
  155.   sd: {
  156.     t: {
  157.       start: 0,
  158.       mid: 0,
  159.       end: 0,
  160.       double: 0
  161.     },
  162.     c: {
  163.       start: 0,
  164.       mid: 0,
  165.       end: 0,
  166.       double: 0
  167.     }
  168.   },
  169.   min: {
  170.     t: {
  171.       start: 0,
  172.       mid: 0,
  173.       end: 0,
  174.       double: 0
  175.     },
  176.     c: {
  177.       start: 0,
  178.       mid: 0,
  179.       end: 0,
  180.       double: 0
  181.     }
  182.   },
  183.   max: {
  184.     t: {
  185.       start: 0,
  186.       mid: 0,
  187.       end: 0,
  188.       double: 0
  189.     },
  190.     c: {
  191.       start: 0,
  192.       mid: 0,
  193.       end: 0,
  194.       double: 0
  195.     }
  196.   }
  197. }
  198.  
  199. const calcTemplate = function (name) {
  200.   for (const v of o.t[name]) {
  201.     calcs.sum.t[name] += v;
  202.   }
  203.   calcs.avg.t[name] = calcs.sum.t[name] / RUNS;
  204.   for (const value of o.t[name]) {
  205.     const v = value - calcs.avg.t[name];
  206.     calcs.sd.t[name] += v*v;
  207.   }
  208.   calcs.sd.t[name] = calcs.sd.t[name] / RUNS;
  209.   calcs.sd.t[name] = Math.pow(calcs.sd.t[name], 0.5);
  210.   calcs.min.t[name] = Math.min(...o.t[name]);
  211.   calcs.max.t[name] = Math.max(...o.t[name]);
  212. }
  213.  
  214. const calcConcat = function (name) {
  215.   for (const v of o.c[name]) {
  216.     calcs.sum.c[name] += v;
  217.   }
  218.   calcs.avg.c[name] = calcs.sum.c[name] / RUNS;
  219.   for (const value of o.c[name]) {
  220.     const v = value - calcs.avg.c[name];
  221.     calcs.sd.c[name] += v*v;
  222.   }
  223.   calcs.sd.c[name] = calcs.sd.c[name] / RUNS;
  224.   calcs.sd.c[name] = Math.pow(calcs.sd.c[name], 0.5);
  225.   calcs.min.c[name] = Math.min(...o.c[name]);
  226.   calcs.max.c[name] = Math.max(...o.c[name]);
  227. }
  228.  
  229. const calc = function () {
  230.   calcTemplate('start');
  231.   calcTemplate('mid');
  232.   calcTemplate('end');
  233.   calcTemplate('double');
  234.  
  235.   calcConcat('start');
  236.   calcConcat('mid');
  237.   calcConcat('end');
  238.   calcConcat('double');
  239. }
  240.  
  241. calc();
  242. console.log(calcs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement