|
| 1 | +#include <assert.h> |
| 2 | +#include <node_api.h> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +struct BenchmarkParams { |
| 6 | + napi_value count_val; |
| 7 | + napi_value bench_obj; |
| 8 | + napi_value start_fn; |
| 9 | + napi_value end_fn; |
| 10 | + uint32_t count; |
| 11 | +}; |
| 12 | + |
| 13 | +static BenchmarkParams ParseBenchmarkArgs(napi_env env, |
| 14 | + const napi_callback_info info) { |
| 15 | + BenchmarkParams params; |
| 16 | + size_t argc = 4; |
| 17 | + napi_value args[4]; |
| 18 | + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); |
| 19 | + |
| 20 | + params.count_val = args[0]; |
| 21 | + params.bench_obj = args[1]; |
| 22 | + params.start_fn = args[2]; |
| 23 | + params.end_fn = args[3]; |
| 24 | + |
| 25 | + napi_get_value_uint32(env, params.count_val, ¶ms.count); |
| 26 | + return params; |
| 27 | +} |
| 28 | + |
| 29 | +static napi_value global_names[20]; |
| 30 | +static napi_value global_values[20]; |
| 31 | +static bool global_properties_initialized = false; |
| 32 | + |
| 33 | +// Creating with many options because complains are when ~20 properties |
| 34 | +static void InitializeTestProperties(napi_env env) { |
| 35 | + if (global_properties_initialized) return; |
| 36 | + |
| 37 | + for (int i = 0; i < 20; i++) { |
| 38 | + std::string name = "foo" + std::to_string(i); |
| 39 | + napi_create_string_utf8( |
| 40 | + env, name.c_str(), NAPI_AUTO_LENGTH, &global_names[i]); |
| 41 | + napi_create_string_utf8( |
| 42 | + env, name.c_str(), NAPI_AUTO_LENGTH, &global_values[i]); |
| 43 | + } |
| 44 | + global_properties_initialized = true; |
| 45 | +} |
| 46 | + |
| 47 | +static napi_value CreateObjectWithPropertiesNew(napi_env env, |
| 48 | + napi_callback_info info) { |
| 49 | + BenchmarkParams params = ParseBenchmarkArgs(env, info); |
| 50 | + |
| 51 | + InitializeTestProperties(env); |
| 52 | + |
| 53 | + napi_value null_prototype; |
| 54 | + napi_get_null(env, &null_prototype); |
| 55 | + |
| 56 | + napi_call_function( |
| 57 | + env, params.bench_obj, params.start_fn, 0, nullptr, nullptr); |
| 58 | + |
| 59 | + for (uint32_t i = 0; i < params.count; i++) { |
| 60 | + napi_value obj; |
| 61 | + napi_create_object_with_properties( |
| 62 | + env, null_prototype, global_names, global_values, 20, &obj); |
| 63 | + } |
| 64 | + |
| 65 | + napi_call_function( |
| 66 | + env, params.bench_obj, params.end_fn, 1, ¶ms.count_val, nullptr); |
| 67 | + |
| 68 | + return nullptr; |
| 69 | +} |
| 70 | + |
| 71 | +static napi_value CreateObjectWithPropertiesOld(napi_env env, |
| 72 | + napi_callback_info info) { |
| 73 | + BenchmarkParams params = ParseBenchmarkArgs(env, info); |
| 74 | + |
| 75 | + InitializeTestProperties(env); |
| 76 | + |
| 77 | + napi_call_function( |
| 78 | + env, params.bench_obj, params.start_fn, 0, nullptr, nullptr); |
| 79 | + |
| 80 | + for (uint32_t i = 0; i < params.count; i++) { |
| 81 | + napi_value obj; |
| 82 | + napi_create_object(env, &obj); |
| 83 | + for (int j = 0; j < 20; j++) { |
| 84 | + napi_set_property(env, obj, global_names[j], global_values[j]); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + napi_call_function( |
| 89 | + env, params.bench_obj, params.end_fn, 1, ¶ms.count_val, nullptr); |
| 90 | + |
| 91 | + return nullptr; |
| 92 | +} |
| 93 | + |
| 94 | +NAPI_MODULE_INIT() { |
| 95 | + napi_property_descriptor desc[] = { |
| 96 | + {"createObjectWithPropertiesNew", |
| 97 | + 0, |
| 98 | + CreateObjectWithPropertiesNew, |
| 99 | + 0, |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + napi_default, |
| 103 | + 0}, |
| 104 | + {"createObjectWithPropertiesOld", |
| 105 | + 0, |
| 106 | + CreateObjectWithPropertiesOld, |
| 107 | + 0, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + napi_default, |
| 111 | + 0}, |
| 112 | + }; |
| 113 | + |
| 114 | + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); |
| 115 | + return exports; |
| 116 | +} |
0 commit comments