From eda8ccd910502219b03e087438258a9491b8a47d Mon Sep 17 00:00:00 2001 From: JonasBa Date: Wed, 29 Apr 2026 19:57:30 -0700 Subject: [PATCH] fix: use integer arithmetic for timestamp conversions and emit elapsed_since_start_ns as string - Replace `* 1e3` with `* 1000` and `* 1e-3` with `/ 1000` for microsecond-to-nanosecond and microsecond-to-millisecond conversions. The floating-point literals promoted uint64_t to double and back, which is unnecessary for integer-to-integer conversion. - Emit elapsed_since_start_ns as a string via napi_create_string_utf8 instead of napi_create_int64. The TypeScript type declares this field as string, and nanosecond values can exceed Number.MAX_SAFE_INTEGER for long profiles. Co-Authored-By: Claude Opus 4.6 --- bindings/cpu_profiler.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bindings/cpu_profiler.cc b/bindings/cpu_profiler.cc index bf37628..04f17de 100644 --- a/bindings/cpu_profiler.cc +++ b/bindings/cpu_profiler.cc @@ -566,7 +566,8 @@ napi_value CreateSample(const napi_env &env, const enum ProfileFormat format, switch (format) { case ProfileFormat::kFormatThread: { napi_value timestamp; - napi_create_int64(env, sample_timestamp_ns, ×tamp); + napi_create_string_utf8(env, std::to_string(sample_timestamp_ns).c_str(), + NAPI_AUTO_LENGTH, ×tamp); napi_set_named_property(env, js_node, "elapsed_since_start_ns", timestamp); } break; case ProfileFormat::kFormatChunk: { @@ -641,9 +642,8 @@ static void GetSamples(const napi_env &env, const v8::CpuProfile *profile, } uint64_t sample_delta_us = sample_timestamp_us - profile_start_time_us; - uint64_t sample_timestamp_ns = sample_delta_us * 1e3; - uint64_t sample_offset_from_profile_start_ms = - (sample_timestamp_us - profile_start_time_us) * 1e-3; + uint64_t sample_timestamp_ns = sample_delta_us * 1000; + uint64_t sample_offset_from_profile_start_ms = sample_delta_us / 1000; double seconds_since_start = (profile_start_timestamp_ms + sample_offset_from_profile_start_ms) * 1e-3;