Skip to content

Commit 8b320a4

Browse files
edusperoniNathanWalker
authored andcommitted
feat: use monotonic time for performance object
1 parent 4e6b5f3 commit 8b320a4

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: NativeScript/runtime/Runtime.h

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class Runtime {
7777
std::unique_ptr<ModuleInternal> moduleInternal_;
7878
int workerId_;
7979
CFRunLoopRef runtimeLoop_;
80+
double startTime;
81+
double realtimeOrigin;
8082
// TODO: refactor this. This is only needed because, during program
8183
// termination (UIApplicationMain not called) the Cache::Workers is released
8284
// (static initialization order fiasco

Diff for: NativeScript/runtime/Runtime.mm

+9-5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
154154
v8Initialized_ = true;
155155
}
156156

157+
startTime = platform_->MonotonicallyIncreasingTime();
158+
realtimeOrigin = platform_->CurrentClockTimeMillis();
159+
157160
// auto version = v8::V8::GetVersion();
158161

159162
Isolate::CreateParams create_params;
@@ -389,16 +392,17 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
389392
Local<FunctionTemplate> nowFuncTemplate = FunctionTemplate::New(isolate, PerformanceNowCallback);
390393
performanceTemplate->Set(tns::ToV8String(isolate, "now"), nowFuncTemplate);
391394

395+
performanceTemplate->Set(tns::ToV8String(isolate, "timeOrigin"),
396+
v8::Number::New(isolate, realtimeOrigin));
397+
392398
Local<v8::String> performancePropertyName = ToV8String(isolate, "performance");
393399
globalTemplate->Set(performancePropertyName, performanceTemplate);
394400
}
395401

396402
void Runtime::PerformanceNowCallback(const FunctionCallbackInfo<Value>& args) {
397-
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
398-
std::chrono::milliseconds timestampMs =
399-
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
400-
double result = timestampMs.count();
401-
args.GetReturnValue().Set(result);
403+
auto runtime = Runtime::GetRuntime(args.GetIsolate());
404+
args.GetReturnValue().Set(
405+
(runtime->platform_->MonotonicallyIncreasingTime() - runtime->startTime) * 1000.0);
402406
}
403407

404408
void Runtime::DefineNativeScriptVersion(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {

0 commit comments

Comments
 (0)