Home > Blockchain >  Does Javascript support any special syntax to support performance testing?
Does Javascript support any special syntax to support performance testing?

Time:12-03

I need to track how long many processes take and so my code looks like this:

console.log("Doing thing 1");
var startTime = performance.now();
var thing = doThing()
times.loadTime = performance.now() - startTime;

console.log("Doing thing 2");
startTime = performance.now();
var thing2 = doOtherThing();
var endTime = performance.now();
times.thingTime2 = endTime - startTime;

console.log("Doing thing 3");
var thing3 = doThing3();
ties.thingTime3 = performance.now() - endTime;

As you can guess, this is getting hard to read.

I know that in my current environment I can use things like:

console.startTime();
console.startTime("Thing 1");
console.endTime();

And so on. See the console object.

But I think I've seen something where javascript code was using blocks or labels like this:

thing1 {
   var thing = doThing()
}

thing2 {
    var thing2 = doOtherThing();
}

thing3 {
    var thing3 = doThing3();
}

// desired result { name: "thing1", startTime: 0, endTime: 10 }
console.log(thing1);

Or labels:

thing1:
    var thing = doThing()
end

These look much more readable to me.

Is there a way to do this in JavaScript?

CodePudding user response:

JavaScript does not have a built-in syntax for performance testing. However, you can use the performance.now() method, as you have done in your code, to measure the time it takes for your code to run.

If you want to make your code more readable, you can use a library like console.time to measure the time it takes for your code to run. This library provides methods like console.time and console.timeEnd that you can use to label your code blocks and measure the time it takes for them to run. Here's an example of how you can use these methods:

console.time("Doing thing 1");
var thing = doThing()
console.timeEnd("Doing thing 1");

console.time("Doing thing 2");
var thing2 = doOtherThing();
console.timeEnd("Doing thing 2");

console.time("Doing thing 3");
var thing3 = doThing3();
console.timeEnd("Doing thing 3");

This will print the time it takes for each code block to run in the console, using the labels you provided.

CodePudding user response:

There's nothing built-in like that, but something that could help would be to write a function wrapper around performance.now calls.

const times = [];
const measure = (cb) => {
  const startTime = performance.now();
  const result = cb();
  times.push(performance.now() - startTime);
  return result;
};

const thing = measure(() => ({ thing1: 'foo' }));
const thing2 = measure(() => {
  for (let i = 0; i < 1e7; i  );
  return { thing2: 'foo' };
});
const thing3 = measure(() => ({ thing3: 'foo' }));
console.log(times);

or

const times = [];
const measure = (name, cb) => {
  const startTime = performance.now();
  const result = cb();
  times.push({ name, time: performance.now() - startTime });
  return result;
};

const thing = measure('thing', () => ({ thing1: 'foo' }));
const thing2 = measure('thing2', () => {
  for (let i = 0; i < 1e7; i  );
  return { thing2: 'foo' };
});
const thing3 = measure('thing3', () => ({ thing3: 'foo' }));
console.log(times);

-

CodePudding user response:

you can use a function like this

const useMeasure = (fn) => {
    const start = performance.now();
    const res = fn();
    const end = performance.now();
    return [res, end - start];
}

it returns the result of the function as well as the time it took to complete.

you can use it like this:-

function count() {
    let cnt = 0;
    for (let i = 0; i < 100000; i  ) {
        cnt  ;
    }
    return cnt;
}

const [result, timeTaken] = useMeasure(count);

console.log(result, timeTaken);

if your function takes arguments you can edit it like this so that you can also pass your args:-

const useMeasureWithArgs = (fn, ...args) => {
    const start = performance.now();
    const res = fn(...args);
    const end = performance.now();
    return [res, end - start];
}

and use it like this:-

function countUpTo(x) {
    let cnt = 0;
    for (let i = 0; i < x; i  ) {
        cnt  ;
    }
    return cnt;
}

const [result, timeTaken] = useMeasureWithArgs(countUpTo, 1000);

console.log(result, timeTaken);
  • Related