forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.cc
More file actions
116 lines (94 loc) Β· 3.02 KB
/
binding.cc
File metadata and controls
116 lines (94 loc) Β· 3.02 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <assert.h>
#include <node_api.h>
#include <string>
struct BenchmarkParams {
napi_value count_val;
napi_value bench_obj;
napi_value start_fn;
napi_value end_fn;
uint32_t count;
};
static BenchmarkParams ParseBenchmarkArgs(napi_env env,
const napi_callback_info info) {
BenchmarkParams params;
size_t argc = 4;
napi_value args[4];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
params.count_val = args[0];
params.bench_obj = args[1];
params.start_fn = args[2];
params.end_fn = args[3];
napi_get_value_uint32(env, params.count_val, ¶ms.count);
return params;
}
static napi_value global_names[20];
static napi_value global_values[20];
static bool global_properties_initialized = false;
// Creating with many options because complains are when ~20 properties
static void InitializeTestProperties(napi_env env) {
if (global_properties_initialized) return;
for (int i = 0; i < 20; i++) {
std::string name = "foo" + std::to_string(i);
napi_create_string_utf8(
env, name.c_str(), NAPI_AUTO_LENGTH, &global_names[i]);
napi_create_string_utf8(
env, name.c_str(), NAPI_AUTO_LENGTH, &global_values[i]);
}
global_properties_initialized = true;
}
static napi_value CreateObjectWithPropertiesNew(napi_env env,
napi_callback_info info) {
BenchmarkParams params = ParseBenchmarkArgs(env, info);
InitializeTestProperties(env);
napi_value null_prototype;
napi_get_null(env, &null_prototype);
napi_call_function(
env, params.bench_obj, params.start_fn, 0, nullptr, nullptr);
for (uint32_t i = 0; i < params.count; i++) {
napi_value obj;
napi_create_object_with_properties(
env, null_prototype, global_names, global_values, 20, &obj);
}
napi_call_function(
env, params.bench_obj, params.end_fn, 1, ¶ms.count_val, nullptr);
return nullptr;
}
static napi_value CreateObjectWithPropertiesOld(napi_env env,
napi_callback_info info) {
BenchmarkParams params = ParseBenchmarkArgs(env, info);
InitializeTestProperties(env);
napi_call_function(
env, params.bench_obj, params.start_fn, 0, nullptr, nullptr);
for (uint32_t i = 0; i < params.count; i++) {
napi_value obj;
napi_create_object(env, &obj);
for (int j = 0; j < 20; j++) {
napi_set_property(env, obj, global_names[j], global_values[j]);
}
}
napi_call_function(
env, params.bench_obj, params.end_fn, 1, ¶ms.count_val, nullptr);
return nullptr;
}
NAPI_MODULE_INIT() {
napi_property_descriptor desc[] = {
{"createObjectWithPropertiesNew",
0,
CreateObjectWithPropertiesNew,
0,
0,
0,
napi_default,
0},
{"createObjectWithPropertiesOld",
0,
CreateObjectWithPropertiesOld,
0,
0,
0,
napi_default,
0},
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}