Skip to content

Commit

Permalink
fix(ext/console): more precision in console.time (#25723)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Sep 19, 2024
1 parent 236a298 commit 159ac45
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions ext/console/01_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -83,6 +84,7 @@ const {
NumberIsInteger,
NumberIsNaN,
NumberParseInt,
NumberPrototypeToFixed,
NumberPrototypeToString,
NumberPrototypeValueOf,
ObjectAssign,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -3331,7 +3345,7 @@ class Console {
return;
}

MapPrototypeSet(timerMap, label, DateNow());
MapPrototypeSet(timerMap, label, currentTime());
};

timeLog = (label = "default", ...args) => {
Expand All @@ -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));
};
Expand All @@ -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`);
};
Expand Down

0 comments on commit 159ac45

Please sign in to comment.