如何舍入 console.time 日志?
How do I round console.time logs?
当我使用 console.time
和 console.timeEnd
来测量 JavaScript 中函数或代码片段的执行速度时,它会将其打印到控制台:
timer: 14657.580078125 ms
如何将其四舍五入为最接近的整数或其他数字?我已经查看了这两个函数的文档,但都没有告诉我如何执行此操作。
不幸的是,除了您已经看到的输出 独占 [=23],您将无法获得 console.time
和 console.timeEnd
(控制台对象的方法) =] 在控制台中。
也许 console.timeEnd
returns undefined
而不是 return 与输入到控制台的相同持续时间太糟糕了。但是,即使它做了 return 那个持续时间,并且你设置了一个变量(并四舍五入),你的控制台仍然会在日志中显示那些未四舍五入的值。如果不破解代码运行的每个 javascript 引擎,就无法改变这种行为,这是不切实际的。
对于四舍五入的值,您必须忘记 console.time
和 console.timeEnd
。那些没用。
使用 Performance
API 效果会更好,因为 console
计时器不会以编程方式公开。
您可以使用 API 做一些事情。您可以使用高分辨率时间戳:
let ts = performance.now();
//some code later
let measure = performance.now() - ts;
这将使您获得 console
计时器所具有的小数格式的时间(以毫秒为单位)。如果你需要在几秒钟内完成,那么你也可以这样做:
console.log(Math.round(measure/1000));
Performance
api 还有其他几种标记时间戳和衡量差异的方法。 Take a look at it.
取自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);
当我使用 console.time
和 console.timeEnd
来测量 JavaScript 中函数或代码片段的执行速度时,它会将其打印到控制台:
timer: 14657.580078125 ms
如何将其四舍五入为最接近的整数或其他数字?我已经查看了这两个函数的文档,但都没有告诉我如何执行此操作。
不幸的是,除了您已经看到的输出 独占 [=23],您将无法获得 console.time
和 console.timeEnd
(控制台对象的方法) =] 在控制台中。
也许 console.timeEnd
returns undefined
而不是 return 与输入到控制台的相同持续时间太糟糕了。但是,即使它做了 return 那个持续时间,并且你设置了一个变量(并四舍五入),你的控制台仍然会在日志中显示那些未四舍五入的值。如果不破解代码运行的每个 javascript 引擎,就无法改变这种行为,这是不切实际的。
对于四舍五入的值,您必须忘记 console.time
和 console.timeEnd
。那些没用。
使用 Performance
API 效果会更好,因为 console
计时器不会以编程方式公开。
您可以使用 API 做一些事情。您可以使用高分辨率时间戳:
let ts = performance.now();
//some code later
let measure = performance.now() - ts;
这将使您获得 console
计时器所具有的小数格式的时间(以毫秒为单位)。如果你需要在几秒钟内完成,那么你也可以这样做:
console.log(Math.round(measure/1000));
Performance
api 还有其他几种标记时间戳和衡量差异的方法。 Take a look at it.
取自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);