-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
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 5 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
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/ |
157 changes: 157 additions & 0 deletions
157
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,157 @@ | ||
| #include <assert.h> | ||
| #include <node_api.h> | ||
|
|
||
| // Creating with many options because complains are when ~20 properties | ||
| static void CreateTestProperties(napi_env env, | ||
| napi_value names[20], | ||
| napi_value values[20]) { | ||
| napi_create_string_utf8(env, "foo", NAPI_AUTO_LENGTH, &names[0]); | ||
| napi_create_string_utf8(env, "value1", NAPI_AUTO_LENGTH, &values[0]); | ||
| napi_create_string_utf8(env, "alpha", NAPI_AUTO_LENGTH, &names[1]); | ||
| napi_create_int32(env, 100, &values[1]); | ||
| napi_create_string_utf8(env, "beta", NAPI_AUTO_LENGTH, &names[2]); | ||
| napi_get_boolean(env, true, &values[2]); | ||
| napi_create_string_utf8(env, "gamma", NAPI_AUTO_LENGTH, &names[3]); | ||
| napi_create_double(env, 3.14159, &values[3]); | ||
| napi_create_string_utf8(env, "delta", NAPI_AUTO_LENGTH, &names[4]); | ||
| napi_create_int32(env, 42, &values[4]); | ||
| napi_create_string_utf8(env, "epsilon", NAPI_AUTO_LENGTH, &names[5]); | ||
| napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &values[5]); | ||
| napi_create_string_utf8(env, "zeta", NAPI_AUTO_LENGTH, &names[6]); | ||
| napi_create_string_utf8(env, "data", NAPI_AUTO_LENGTH, &values[6]); | ||
| napi_create_string_utf8(env, "eta", NAPI_AUTO_LENGTH, &names[7]); | ||
| napi_create_string_utf8(env, "info", NAPI_AUTO_LENGTH, &values[7]); | ||
| napi_create_string_utf8(env, "theta", NAPI_AUTO_LENGTH, &names[8]); | ||
| napi_create_string_utf8(env, "sample", NAPI_AUTO_LENGTH, &values[8]); | ||
| napi_create_string_utf8(env, "iota", NAPI_AUTO_LENGTH, &names[9]); | ||
| napi_create_double(env, 2.71828, &values[9]); | ||
| napi_create_string_utf8(env, "kappa", NAPI_AUTO_LENGTH, &names[10]); | ||
| napi_create_string_utf8(env, "benchmark", NAPI_AUTO_LENGTH, &values[10]); | ||
| napi_create_string_utf8(env, "lambda", NAPI_AUTO_LENGTH, &names[11]); | ||
| napi_create_string_utf8(env, "result", NAPI_AUTO_LENGTH, &values[11]); | ||
| napi_create_string_utf8(env, "mu", NAPI_AUTO_LENGTH, &names[12]); | ||
| napi_create_string_utf8(env, "output", NAPI_AUTO_LENGTH, &values[12]); | ||
| napi_create_string_utf8(env, "nu", NAPI_AUTO_LENGTH, &names[13]); | ||
| napi_get_boolean(env, false, &values[13]); | ||
| napi_create_string_utf8(env, "xi", NAPI_AUTO_LENGTH, &names[14]); | ||
| napi_create_int32(env, 7, &values[14]); | ||
| napi_create_string_utf8(env, "omicron", NAPI_AUTO_LENGTH, &names[15]); | ||
| napi_create_double(env, 1.618, &values[15]); | ||
| napi_create_string_utf8(env, "pi", NAPI_AUTO_LENGTH, &names[16]); | ||
| napi_create_string_utf8(env, "config", NAPI_AUTO_LENGTH, &values[16]); | ||
| napi_create_string_utf8(env, "rho", NAPI_AUTO_LENGTH, &names[17]); | ||
| napi_create_int32(env, 999, &values[17]); | ||
| napi_create_string_utf8(env, "sigma", NAPI_AUTO_LENGTH, &names[18]); | ||
| napi_create_double(env, 0.577, &values[18]); | ||
| napi_create_string_utf8(env, "tau", NAPI_AUTO_LENGTH, &names[19]); | ||
| napi_get_boolean(env, true, &values[19]); | ||
| } | ||
|
|
||
| static napi_value CreateObjectWithPropertiesNew(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 4; | ||
| napi_value args[4]; | ||
| napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); | ||
|
|
||
| napi_value count_val = args[0]; | ||
| napi_value bench_obj = args[1]; | ||
| napi_value start_fn = args[2]; | ||
| napi_value end_fn = args[3]; | ||
|
|
||
| uint32_t count; | ||
| napi_get_value_uint32(env, count_val, &count); | ||
|
|
||
| napi_value names[20]; | ||
| napi_value values[20]; | ||
| napi_value null_prototype; | ||
|
|
||
| napi_get_null(env, &null_prototype); | ||
| CreateTestProperties(env, names, values); | ||
|
|
||
| napi_call_function(env, bench_obj, start_fn, 0, nullptr, nullptr); | ||
|
|
||
| for (uint32_t i = 0; i < count; i++) { | ||
| napi_value obj; | ||
| napi_create_object_with_properties( | ||
| env, null_prototype, names, values, 20, &obj); | ||
| } | ||
|
|
||
| napi_call_function(env, bench_obj, end_fn, 1, &count_val, nullptr); | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| static napi_value CreateObjectWithPropertiesOld(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 4; | ||
| napi_value args[4]; | ||
| napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); | ||
|
|
||
| napi_value count_val = args[0]; | ||
| napi_value bench_obj = args[1]; | ||
| napi_value start_fn = args[2]; | ||
| napi_value end_fn = args[3]; | ||
|
|
||
| uint32_t count; | ||
| napi_get_value_uint32(env, count_val, &count); | ||
|
|
||
| napi_value names[20]; | ||
| napi_value values[20]; | ||
|
|
||
| CreateTestProperties(env, names, values); | ||
|
|
||
| napi_call_function(env, bench_obj, start_fn, 0, nullptr, nullptr); | ||
|
|
||
| for (uint32_t i = 0; i < count; i++) { | ||
| napi_value obj; | ||
| napi_create_object(env, &obj); | ||
| napi_set_property(env, obj, names[0], values[0]); | ||
| napi_set_property(env, obj, names[1], values[1]); | ||
| napi_set_property(env, obj, names[2], values[2]); | ||
| napi_set_property(env, obj, names[3], values[3]); | ||
| napi_set_property(env, obj, names[4], values[4]); | ||
| napi_set_property(env, obj, names[5], values[5]); | ||
| napi_set_property(env, obj, names[6], values[6]); | ||
| napi_set_property(env, obj, names[7], values[7]); | ||
| napi_set_property(env, obj, names[8], values[8]); | ||
| napi_set_property(env, obj, names[9], values[9]); | ||
| napi_set_property(env, obj, names[10], values[10]); | ||
| napi_set_property(env, obj, names[11], values[11]); | ||
| napi_set_property(env, obj, names[12], values[12]); | ||
| napi_set_property(env, obj, names[13], values[13]); | ||
| napi_set_property(env, obj, names[14], values[14]); | ||
| napi_set_property(env, obj, names[15], values[15]); | ||
| napi_set_property(env, obj, names[16], values[16]); | ||
| napi_set_property(env, obj, names[17], values[17]); | ||
| napi_set_property(env, obj, names[18], values[18]); | ||
| napi_set_property(env, obj, names[19], values[19]); | ||
| } | ||
|
|
||
| napi_call_function(env, bench_obj, end_fn, 1, &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_VERSION=8'] | ||
miguelmarcondesf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ] | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2637,6 +2637,41 @@ It is the equivalent of doing `new Object()` in JavaScript. | |
| The JavaScript `Object` type is described in [Section object type][] of the | ||
| ECMAScript Language Specification. | ||
|
|
||
| #### `napi_create_object_with_properties` | ||
|
|
||
| <!-- YAML | ||
| added: REPLACEME | ||
| napiVersion: 8 | ||
| --> | ||
|
|
||
miguelmarcondesf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```c | ||
| napi_status napi_create_object_with_properties(napi_env env, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| napi_value prototype_or_null, | ||
| const napi_value* property_names, | ||
| const napi_value* property_values, | ||
| size_t property_count, | ||
| napi_value* result) | ||
| ``` | ||
|
|
||
| * `[in] env`: The environment that the API is invoked under. | ||
| * `[in] prototype_or_null`: The prototype object for the new object, or `null` | ||
| for no prototype. | ||
miguelmarcondesf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * `[in] property_names`: Array of `napi_value` representing the property names. | ||
| * `[in] property_values`: Array of `napi_value` representing the property values. | ||
| * `[in] property_count`: Number of properties in the arrays. | ||
| * `[out] result`: A `napi_value` representing a JavaScript `Object`. | ||
|
|
||
| Returns `napi_ok` if the API succeeded. | ||
|
|
||
| This API creates a JavaScript `Object` with the specified prototype and | ||
| properties. This is more efficient than calling `napi_create_object` followed | ||
| by multiple `napi_set_property` calls, as it can create the object with all | ||
| properties atomically, avoiding potential V8 map transitions. | ||
|
|
||
| The arrays `property_names` and `property_values` must have the same length | ||
| specified by `property_count`. The properties are added to the object in the | ||
| order they appear in the arrays. | ||
|
|
||
| #### `napi_create_symbol` | ||
|
|
||
| <!-- YAML | ||
|
|
||
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.