Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ITERATIONS = 100000000;
- const RUNS = 100;
- const templateStartString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += `${i}def`.length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const templateMidString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += `abc${i}def`.length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const templateEndString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += `abc${i}`.length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const templateDoubleString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += `abc${i}def${i}ghi`.length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const concatStartString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += (i + 'def').length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const concatMidString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += ('abc' + i + 'def').length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const concatEndString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += ('abc' + i).length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const concatDoubleString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += ('abc' + i + 'def' + i + 'ghi').length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const o = {
- t: {
- start: [],
- mid: [],
- end: [],
- double: []
- },
- c: {
- start: [],
- mid: [],
- end: [],
- double: []
- }
- }
- for (let i = 0; i < RUNS; i++) {
- console.log(`RUN ${i+1}`);
- o.t.start.push(templateStartString());
- o.t.mid.push(templateMidString());
- o.t.end.push(templateEndString());
- o.t.double.push(templateDoubleString());
- o.c.start.push(concatStartString());
- o.c.mid.push(concatMidString());
- o.c.end.push(concatEndString());
- o.c.double.push(concatDoubleString());
- }
- const calcs = {
- sum: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- }
- },
- avg: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- }
- },
- sd: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- }
- },
- min: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- }
- },
- max: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0
- }
- }
- }
- const calcTemplate = function (name) {
- for (const v of o.t[name]) {
- calcs.sum.t[name] += v;
- }
- calcs.avg.t[name] = calcs.sum.t[name] / RUNS;
- for (const value of o.t[name]) {
- const v = value - calcs.avg.t[name];
- calcs.sd.t[name] += v*v;
- }
- calcs.sd.t[name] = calcs.sd.t[name] / RUNS;
- calcs.sd.t[name] = Math.pow(calcs.sd.t[name], 0.5);
- calcs.min.t[name] = Math.min(...o.t[name]);
- calcs.max.t[name] = Math.max(...o.t[name]);
- }
- const calcConcat = function (name) {
- for (const v of o.c[name]) {
- calcs.sum.c[name] += v;
- }
- calcs.avg.c[name] = calcs.sum.c[name] / RUNS;
- for (const value of o.c[name]) {
- const v = value - calcs.avg.c[name];
- calcs.sd.c[name] += v*v;
- }
- calcs.sd.c[name] = calcs.sd.c[name] / RUNS;
- calcs.sd.c[name] = Math.pow(calcs.sd.c[name], 0.5);
- calcs.min.c[name] = Math.min(...o.c[name]);
- calcs.max.c[name] = Math.max(...o.c[name]);
- }
- const calc = function () {
- calcTemplate('start');
- calcTemplate('mid');
- calcTemplate('end');
- calcTemplate('double');
- calcConcat('start');
- calcConcat('mid');
- calcConcat('end');
- calcConcat('double');
- }
- calc();
- console.log(calcs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement