javascript - How do I round console.time logs? - Stack Overflow

When I use console.time and console.timeEnd to measure the execution speed of a function or code snippe

When I use console.time and console.timeEnd to measure the execution speed of a function or code snippet in JavaScript it prints this to the console:

timer: 14657.580078125 ms

How do I round this to the nearest integer or some other digit? I've looked at the documentation for both of these functions and neither gives me a clue how to do this.

When I use console.time and console.timeEnd to measure the execution speed of a function or code snippet in JavaScript it prints this to the console:

timer: 14657.580078125 ms

How do I round this to the nearest integer or some other digit? I've looked at the documentation for both of these functions and neither gives me a clue how to do this.

Share asked May 6, 2021 at 15:21 leo848leo848 6672 gold badges7 silver badges27 bronze badges 4
  • You can use ceiling function to round the output developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Ezio Commented May 6, 2021 at 15:23
  • 4 You don't. Why would you? It's only printed to the console anyway, you can't do anything with that number in your program. – Bergi Commented May 6, 2021 at 15:23
  • 2 @ABC That gives NaN, as console.timeEnd() returns undefined – Bergi Commented May 6, 2021 at 15:50
  • node uses 2.005s, Edge (and Chrome) uses 2001.93701171875 ms which is not a good default. I've filed a ticket with the Edge team to make this configurable or have a more reasonable default. – mikemaccana Commented May 12, 2023 at 21:29
Add a ment  | 

2 Answers 2

Reset to default 7

You'd have a better shot with the Performance API, since console timers are not exposed programmatically.

There are a couple of things you could do with the API. You could use a high resolution timestamp:

let ts = performance.now();
//some code later
let measure = performance.now() - ts;

That would get you the time in milliseconds with the decimals format you have with console timers. If you need it on seconds then you can just as well do:

console.log(Math.round(measure/1000));

The Performance api has several other way to mark timestamps and measure the difference. Take a look at it.

An example taken from MDN:

const markerNameA = "example-marker-a"
const markerNameB = "example-marker-b"

// Run some nested timeouts, and create a PerformanceMark for each.
performance.mark(markerNameA);
setTimeout(function() {
  performance.mark(markerNameB);
  setTimeout(function() {

    // Create a variety of measurements.
    performance.measure("measure a to b", markerNameA, markerNameB);
    performance.measure("measure a to now", markerNameA);
    performance.measure("measure from navigation start to b", undefined, markerNameB);
    performance.measure("measure from navigation start to now");

    // Pull out all of the measurements.
    console.log(performance.getEntriesByType("measure"));

    // Finally, clean up the entries.
    performance.clearMarks();
    performance.clearMeasures();
  }, 1000);
}, 1000);

Unfortunately, you will not get console.time and console.timeEnd (methods of the console object) to do anything other than the output you've already seen exclusively in the console.

Perhaps it is too bad that console.timeEnd returns undefined instead of returning the same duration that it puts into the console. But, even if it did return that duration, and you set a variable (and rounded it), your console would still show those un-rounded values in the log. There's no way to change this behavior without hacking each javascript engine your code runs on and that's not practical.

For rounded values, you'll have to forget about console.time and console.timeEnd. Those won't assist.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745056571a4608703.html

相关推荐

  • javascript - How do I round console.time logs? - Stack Overflow

    When I use console.time and console.timeEnd to measure the execution speed of a function or code snippe

    20小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信