-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPerformance.cpp
More file actions
66 lines (53 loc) · 1.71 KB
/
Performance.cpp
File metadata and controls
66 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Performance.h"
#include "mach/mach_time.h"
namespace charon {
void Performance::init(napi_env env) {
napi_value global, Performance, performance;
napi_get_global(env, &global);
const napi_property_descriptor properties[] = {
{
.utf8name = "now",
.name = nullptr,
.method = now,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
};
napi_define_class(env, "Performance", NAPI_AUTO_LENGTH,
Performance::constructor, nullptr, 1, properties,
&Performance);
napi_new_instance(env, Performance, 0, nullptr, &performance);
const napi_property_descriptor globalProperties[] = {
{
.utf8name = "performance",
.name = nullptr,
.method = nullptr,
.getter = nullptr,
.setter = nullptr,
.value = performance,
.attributes = napi_default,
.data = nullptr,
},
};
napi_define_properties(env, global, 1, globalProperties);
}
napi_value Performance::constructor(napi_env env, napi_callback_info cbinfo) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
napi_value Performance::now(napi_env env, napi_callback_info cbinfo) {
uint64_t time = mach_absolute_time();
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
double nanoseconds =
(double)time * (double)timebase.numer / (double)timebase.denom;
double milliseconds = nanoseconds / 1000000.0;
napi_value result;
napi_create_double(env, milliseconds, &result);
return result;
}
} // namespace charon