summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2024-09-19 14:41:05 +0200
committerGitHub <noreply@github.com>2024-09-19 14:41:05 +0200
commit159ac45a852c32b9b76176328900b9cdb3f1a8e0 (patch)
treed713c7a7b3a6a50e79a4b7c711bea3d69fddb29a
parent236a298f9db93a4247449a177e4a021ca121353d (diff)
fix(ext/console): more precision in console.time (#25723)
-rw-r--r--ext/console/01_console.js38
1 files changed, 35 insertions, 3 deletions
diff --git a/ext/console/01_console.js b/ext/console/01_console.js
index 9b5e6fe1a..1ca23d5a4 100644
--- a/ext/console/01_console.js
+++ b/ext/console/01_console.js
@@ -33,6 +33,7 @@ import {
op_get_non_index_property_names,
op_preview_entries,
} from "ext:core/ops";
+import * as ops from "ext:core/ops";
const {
Array,
ArrayBufferPrototypeGetByteLength,
@@ -83,6 +84,7 @@ const {
NumberIsInteger,
NumberIsNaN,
NumberParseInt,
+ NumberPrototypeToFixed,
NumberPrototypeToString,
NumberPrototypeValueOf,
ObjectAssign,
@@ -151,11 +153,23 @@ const {
SymbolPrototypeToString,
SymbolPrototypeValueOf,
SymbolToStringTag,
+ TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeGetLength,
Uint8Array,
+ Uint32Array,
} = primordials;
+let currentTime = DateNow;
+if (ops.op_now) {
+ const hrU8 = new Uint8Array(8);
+ const hr = new Uint32Array(TypedArrayPrototypeGetBuffer(hrU8));
+ currentTime = function opNow() {
+ ops.op_now(hrU8);
+ return (hr[0] * 1000 + hr[1] / 1e6);
+ };
+}
+
let noColorStdout = () => false;
let noColorStderr = () => false;
@@ -3331,7 +3345,7 @@ class Console {
return;
}
- MapPrototypeSet(timerMap, label, DateNow());
+ MapPrototypeSet(timerMap, label, currentTime());
};
timeLog = (label = "default", ...args) => {
@@ -3343,7 +3357,16 @@ class Console {
}
const startTime = MapPrototypeGet(timerMap, label);
- const duration = DateNow() - startTime;
+ let duration = currentTime() - startTime;
+ if (duration < 1) {
+ duration = NumberPrototypeToFixed(duration, 3);
+ } else if (duration < 10) {
+ duration = NumberPrototypeToFixed(duration, 2);
+ } else if (duration < 100) {
+ duration = NumberPrototypeToFixed(duration, 1);
+ } else {
+ duration = NumberPrototypeToFixed(duration, 0);
+ }
this.info(`${label}: ${duration}ms`, ...new SafeArrayIterator(args));
};
@@ -3358,7 +3381,16 @@ class Console {
const startTime = MapPrototypeGet(timerMap, label);
MapPrototypeDelete(timerMap, label);
- const duration = DateNow() - startTime;
+ let duration = currentTime() - startTime;
+ if (duration < 1) {
+ duration = NumberPrototypeToFixed(duration, 3);
+ } else if (duration < 10) {
+ duration = NumberPrototypeToFixed(duration, 2);
+ } else if (duration < 100) {
+ duration = NumberPrototypeToFixed(duration, 1);
+ } else {
+ duration = NumberPrototypeToFixed(duration, 0);
+ }
this.info(`${label}: ${duration}ms`);
};