-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
node-api: Add node_api_create_object_with_properties method #59953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 15 commits into
nodejs:main
from
miguelmarcondesf:45905/node-api-create-object
Oct 30, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b96fca7
node-api: add napi_create_object_with_properties
miguelmarcondesf 69ba24b
test: add tests for creating objects with properties in js-native-api
miguelmarcondesf cdf5313
node-api: add benchmark for new and old creation
miguelmarcondesf 1742953
doc: add napi_create_object_with_properties API
miguelmarcondesf 508a4bd
run format-cpp
miguelmarcondesf f90ad08
benchmark: streamline object creation with properties and improve arg…
miguelmarcondesf 3810e3b
node-api: update to use NAPI_EXPERIMENTAL
miguelmarcondesf 7a51c98
node-api: remove unnecessary branch
miguelmarcondesf 29959e7
node-api: convert nullptr to null
miguelmarcondesf d1c6519
test: asserting object is created with specified property
miguelmarcondesf af3d9ec
doc: add experimental flag
miguelmarcondesf 4ada4c2
test: test prototype with NULL value
miguelmarcondesf 8866c02
test: format NODE_API_CALL
miguelmarcondesf 8de7782
test: remove unnecesary var
miguelmarcondesf 000bfe7
node-api: add experimental flag for new method
miguelmarcondesf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| build/ |
116 changes: 116 additions & 0 deletions
116
benchmark/napi/create_object_with_properties/binding.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| 'targets': [ | ||
| { | ||
| 'target_name': 'binding', | ||
| 'sources': [ 'binding.cc' ], | ||
| 'defines': ['NAPI_EXPERIMENTAL'] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../../common.js'); | ||
|
|
||
| let binding; | ||
| try { | ||
| binding = require(`./build/${common.buildType}/binding`); | ||
| } catch { | ||
| console.error(`${__filename}: Binding failed to load`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1e2, 1e3, 1e4, 1e5, 1e6], | ||
| method: ['new', 'old'], | ||
| }); | ||
|
|
||
| function main({ n, method }) { | ||
| if (method === 'new') { | ||
| binding.createObjectWithPropertiesNew(n, bench, bench.start, bench.end); | ||
| } else { | ||
| binding.createObjectWithPropertiesOld(n, bench, bench.start, bench.end); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.