Skip to content

Commit b29eb69

Browse files
jasonginMylesBorins
authored andcommitted
n-api: optimize number API performance
- Add separate APIs for creating different kinds of numbers, because creating a V8 number value from an integer is faster than creating one from a double. - When getting number values, avoid getting the current context because the context will not actually be used and is expensive to obtain. - When creating values, don't use v8::TryCatch (NAPI_PREAMBLE), because these functions have no possibility of executing JS code. Refs: #14379 Backport-PR-URL: #19447 PR-URL: #14573 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 0ef0b34 commit b29eb69

File tree

18 files changed

+197
-88
lines changed

18 files changed

+197
-88
lines changed

doc/api/n-api.md

+77-8
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,81 @@ JavaScript DataView Objects are described in
13721372
[Section 24.3][] of the ECMAScript Language Specification.
13731373
13741374
### Functions to convert from C types to N-API
1375-
#### *napi_create_number*
1375+
#### *napi_create_int32*
13761376
<!-- YAML
1377-
added: v8.0.0
1377+
added: REPLACEME
1378+
-->
1379+
```C
1380+
napi_status napi_create_int32(napi_env env, int32_t value, napi_value* result)
1381+
```
1382+
1383+
- `[in] env`: The environment that the API is invoked under.
1384+
- `[in] value`: Integer value to be represented in JavaScript.
1385+
- `[out] result`: A `napi_value` representing a JavaScript Number.
1386+
1387+
Returns `napi_ok` if the API succeeded.
1388+
1389+
This API is used to convert from the C `int32_t` type to the JavaScript
1390+
Number type.
1391+
1392+
The JavaScript Number type is described in
1393+
[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type)
1394+
of the ECMAScript Language Specification.
1395+
1396+
#### *napi_create_uint32*
1397+
<!-- YAML
1398+
added: REPLACEME
1399+
-->
1400+
```C
1401+
napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result)
1402+
```
1403+
1404+
- `[in] env`: The environment that the API is invoked under.
1405+
- `[in] value`: Unsigned integer value to be represented in JavaScript.
1406+
- `[out] result`: A `napi_value` representing a JavaScript Number.
1407+
1408+
Returns `napi_ok` if the API succeeded.
1409+
1410+
This API is used to convert from the C `uint32_t` type to the JavaScript
1411+
Number type.
1412+
1413+
The JavaScript Number type is described in
1414+
[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type)
1415+
of the ECMAScript Language Specification.
1416+
1417+
#### *napi_create_int64*
1418+
<!-- YAML
1419+
added: REPLACEME
1420+
-->
1421+
```C
1422+
napi_status napi_create_int64(napi_env env, int64_t value, napi_value* result)
1423+
```
1424+
1425+
- `[in] env`: The environment that the API is invoked under.
1426+
- `[in] value`: Integer value to be represented in JavaScript.
1427+
- `[out] result`: A `napi_value` representing a JavaScript Number.
1428+
1429+
Returns `napi_ok` if the API succeeded.
1430+
1431+
This API is used to convert from the C `int64_t` type to the JavaScript
1432+
Number type.
1433+
1434+
The JavaScript Number type is described in
1435+
[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type)
1436+
of the ECMAScript Language Specification. Note the complete range of `int64_t`
1437+
cannot be represented with full precision in JavaScript. Integer values
1438+
outside the range of
1439+
[`Number.MIN_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.min_safe_integer)
1440+
-(2^53 - 1) -
1441+
[`Number.MAX_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.max_safe_integer)
1442+
(2^53 - 1) will lose precision.
1443+
1444+
#### *napi_create_double*
1445+
<!-- YAML
1446+
added: REPLACEME
13781447
-->
13791448
```C
1380-
napi_status napi_create_number(napi_env env, double value, napi_value* result)
1449+
napi_status napi_create_double(napi_env env, double value, napi_value* result)
13811450
```
13821451
13831452
- `[in] env`: The environment that the API is invoked under.
@@ -1386,7 +1455,7 @@ napi_status napi_create_number(napi_env env, double value, napi_value* result)
13861455
13871456
Returns `napi_ok` if the API succeeded.
13881457
1389-
This API is used to convert from the C double type to the JavaScript
1458+
This API is used to convert from the C `double` type to the JavaScript
13901459
Number type.
13911460
13921461
The JavaScript Number type is described in
@@ -2170,7 +2239,7 @@ status = napi_create_object(env, &obj);
21702239
if (status != napi_ok) return status;
21712240

21722241
// Create a napi_value for 123
2173-
status = napi_create_number(env, 123, &value);
2242+
status = napi_create_int32(env, 123, &value);
21742243
if (status != napi_ok) return status;
21752244

21762245
// obj.myProp = 123
@@ -2244,9 +2313,9 @@ if (status != napi_ok) return status;
22442313

22452314
// Create napi_values for 123 and 456
22462315
napi_value fooValue, barValue;
2247-
status = napi_create_number(env, 123, &fooValue);
2316+
status = napi_create_int32(env, 123, &fooValue);
22482317
if (status != napi_ok) return status;
2249-
status = napi_create_number(env, 456, &barValue);
2318+
status = napi_create_int32(env, 456, &barValue);
22502319
if (status != napi_ok) return status;
22512320

22522321
// Set the properties
@@ -2707,7 +2776,7 @@ status = napi_get_named_property(env, global, "AddTwo", &add_two);
27072776
if (status != napi_ok) return;
27082777

27092778
// const arg = 1337
2710-
status = napi_create_number(env, 1337, &arg);
2779+
status = napi_create_int32(env, 1337, &arg);
27112780
if (status != napi_ok) return;
27122781

27132782
napi_value* argv = &arg;

0 commit comments

Comments
 (0)