From cfa4d960c8acb904f632e2cbdb5bf0c910174f4a Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Wed, 30 Oct 2024 15:57:46 -0300 Subject: [PATCH] src,lib: optimize nodeTiming.uvMetricsInfo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55614 Reviewed-By: Juan José Arboleda Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Vinícius Lourenço Claro Cardoso --- lib/internal/perf/nodetiming.js | 9 ++++++++- src/node_perf.cc | 26 +++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/internal/perf/nodetiming.js b/lib/internal/perf/nodetiming.js index d19bc021263a7f..a9e0c3f252ce5e 100644 --- a/lib/internal/perf/nodetiming.js +++ b/lib/internal/perf/nodetiming.js @@ -128,7 +128,14 @@ class PerformanceNodeTiming { __proto__: null, enumerable: true, configurable: true, - get: uvMetricsInfo, + get: () => { + const metrics = uvMetricsInfo(); + return { + loopCount: metrics[0], + events: metrics[1], + eventsWaiting: metrics[2], + }; + }, }, }); } diff --git a/src/node_perf.cc b/src/node_perf.cc index a868b0e82e0813..d8b26f5c845be1 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -14,6 +14,7 @@ namespace node { namespace performance { +using v8::Array; using v8::Context; using v8::DontDelete; using v8::Function; @@ -264,26 +265,17 @@ void LoopIdleTime(const FunctionCallbackInfo& args) { void UvMetricsInfo(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Isolate* isolate = env->isolate(); uv_metrics_t metrics; - // uv_metrics_info always return 0 CHECK_EQ(uv_metrics_info(env->event_loop(), &metrics), 0); - - Local obj = Object::New(env->isolate()); - obj->Set(env->context(), - env->loop_count(), - Integer::NewFromUnsigned(env->isolate(), metrics.loop_count)) - .Check(); - obj->Set(env->context(), - env->events(), - Integer::NewFromUnsigned(env->isolate(), metrics.events)) - .Check(); - obj->Set(env->context(), - env->events_waiting(), - Integer::NewFromUnsigned(env->isolate(), metrics.events_waiting)) - .Check(); - - args.GetReturnValue().Set(obj); + Local data[] = { + Integer::New(isolate, metrics.loop_count), + Integer::New(isolate, metrics.events), + Integer::New(isolate, metrics.events_waiting), + }; + Local arr = Array::New(env->isolate(), data, arraysize(data)); + args.GetReturnValue().Set(arr); } void CreateELDHistogram(const FunctionCallbackInfo& args) {