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 templateManyString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += `${i}abc${i}def${i}ghi${i}jkl${i}mno${i}pqr${i}stu${i}vwx${i}yz${i}`.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 concatManyString = function () {
- const before = new Date().getTime();
- let l = 0;
- for (let i = 0; i < ITERATIONS; i++) {
- l += (i + 'abc' + i + 'def' + i + 'ghi' + i + 'jkl' + i + 'mno' + i + 'pqr' + i + 'stu' + i + 'vwx' + i + 'yz' + i).length;
- }
- const after = new Date().getTime();
- console.log(l);
- console.log(after-before);
- return after-before;
- }
- const o = {
- t: {
- start: [],
- mid: [],
- end: [],
- double: [],
- many: []
- },
- c: {
- start: [],
- mid: [],
- end: [],
- double: [],
- many: []
- }
- }
- // warm up
- console.log('start warm up');
- concatStartString();
- console.log('warm up done');
- for (let i = 0; i < RUNS; i++) {
- console.log('RUN ' + ('000' + (i+1)).slice(-3));
- o.t.start.push(templateStartString());
- o.t.mid.push(templateMidString());
- o.t.end.push(templateEndString());
- o.t.double.push(templateDoubleString());
- o.t.many.push(templateManyString());
- o.c.start.push(concatStartString());
- o.c.mid.push(concatMidString());
- o.c.end.push(concatEndString());
- o.c.double.push(concatDoubleString());
- o.c.many.push(concatManyString());
- }
- const calcs = {
- sum: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- avg: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- sd: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- aad: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- min: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- max: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- }
- },
- median: {
- t: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 0
- },
- c: {
- start: 0,
- mid: 0,
- end: 0,
- double: 0,
- many: 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.aad.t[name] += Math.abs(v);
- }
- calcs.sd.t[name] = calcs.sd.t[name] / RUNS;
- calcs.aad.t[name] = calcs.aad.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]);
- o.t[name].sort((a,b) => a-b);
- calcs.median.t[name] = (o.t[name][(o.t[name].length - 1) >> 1] + o.t[name][o.t[name].length >> 1]) / 2;
- }
- 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.aad.c[name] += Math.abs(v);
- }
- calcs.sd.c[name] = calcs.sd.c[name] / RUNS;
- calcs.aad.c[name] = calcs.aad.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]);
- calcs.median.c[name] = (o.c[name][(o.c[name].length - 1) >> 1] + o.c[name][o.c[name].length >> 1]) / 2;
- }
- const calc = function () {
- calcTemplate('start');
- calcTemplate('mid');
- calcTemplate('end');
- calcTemplate('double');
- calcTemplate('many');
- calcConcat('start');
- calcConcat('mid');
- calcConcat('end');
- calcConcat('double');
- calcConcat('many');
- }
- calc();
- console.log(JSON.stringify(calcs, null, 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement