Skip to content

Commit aebe2fc

Browse files
joyeecheungRafaelGSS
authored andcommitted
perf_hooks: implement performance.now() with fast API calls
PR-URL: #50492 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 4b26f14 commit aebe2fc

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

benchmark/perf_hooks/now.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e6],
8+
});
9+
10+
function main({ n }) {
11+
const arr = [];
12+
for (let i = 0; i < n; ++i) {
13+
arr.push(performance.now());
14+
}
15+
16+
bench.start();
17+
for (let i = 0; i < n; i++) {
18+
arr[i] = performance.now();
19+
}
20+
bench.end(n);
21+
22+
assert.strictEqual(arr.length, n);
23+
}

lib/internal/perf/utils.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN,
66
},
77
milestones,
8+
now,
89
} = internalBinding('performance');
910

1011
function getTimeOrigin() {
@@ -13,12 +14,6 @@ function getTimeOrigin() {
1314
return milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN] / 1e6;
1415
}
1516

16-
// Returns the time relative to the process start time in milliseconds.
17-
function now() {
18-
const hr = process.hrtime();
19-
return (hr[0] * 1000 + hr[1] / 1e6) - getTimeOrigin();
20-
}
21-
2217
// Returns the milestone relative to the process start time in milliseconds.
2318
function getMilestoneTimestamp(milestoneIdx) {
2419
const ns = milestones[milestoneIdx];

src/node_external_reference.h

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ using CFunctionCallbackWithOneByteString =
1515
using CFunctionCallback = void (*)(v8::Local<v8::Value> receiver);
1616
using CFunctionCallbackReturnDouble =
1717
double (*)(v8::Local<v8::Object> receiver);
18+
using CFunctionCallbackValueReturnDouble =
19+
double (*)(v8::Local<v8::Value> receiver);
1820
using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> receiver,
1921
int64_t);
2022
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
@@ -38,6 +40,7 @@ class ExternalReferenceRegistry {
3840
V(CFunctionCallback) \
3941
V(CFunctionCallbackWithOneByteString) \
4042
V(CFunctionCallbackReturnDouble) \
43+
V(CFunctionCallbackValueReturnDouble) \
4144
V(CFunctionCallbackWithInt64) \
4245
V(CFunctionCallbackWithBool) \
4346
V(CFunctionCallbackWithString) \

src/node_perf.cc

+21
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
291291
performance::NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
292292
}
293293

294+
static double PerformanceNowImpl() {
295+
return static_cast<double>(uv_hrtime() - performance_process_start) /
296+
NANOS_PER_MILLIS;
297+
}
298+
299+
static double FastPerformanceNow(v8::Local<v8::Value> receiver) {
300+
return PerformanceNowImpl();
301+
}
302+
303+
static void SlowPerformanceNow(const FunctionCallbackInfo<Value>& args) {
304+
args.GetReturnValue().Set(PerformanceNowImpl());
305+
}
306+
307+
static v8::CFunction fast_performance_now(
308+
v8::CFunction::Make(FastPerformanceNow));
309+
294310
static void CreatePerIsolateProperties(IsolateData* isolate_data,
295311
Local<ObjectTemplate> target) {
296312
Isolate* isolate = isolate_data->isolate();
@@ -311,6 +327,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
311327
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
312328
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
313329
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
330+
SetFastMethodNoSideEffect(
331+
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
314332
}
315333

316334
void CreatePerContextProperties(Local<Object> target,
@@ -376,6 +394,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
376394
registry->Register(GetTimeOriginTimeStamp);
377395
registry->Register(CreateELDHistogram);
378396
registry->Register(MarkBootstrapComplete);
397+
registry->Register(SlowPerformanceNow);
398+
registry->Register(FastPerformanceNow);
399+
registry->Register(fast_performance_now.GetTypeInfo());
379400
HistogramBase::RegisterExternalReferences(registry);
380401
IntervalHistogram::RegisterExternalReferences(registry);
381402
}

0 commit comments

Comments
 (0)