atom-community / atom-community/zadeh
Faster way to convert a Napi::Array to a vector of string
- Dominant language
- C++
- Stars
- 28
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Description
_Summarized from https://github.com/nodejs/node-addon-api/issues/862_
# The problem
I have written an algorithm that works on a vector string. However, I am not sure if the way I convert a Napi::Array to a vector of the string is the most efficient way to do so. There is no documentation on this topic.
The conversion part currently takes 22ms which is half of the whole execution time of the program!
Here is a simplified example.
```cpp
Napi::Value MyClass::makeVector(const Napi::CallbackInfo &info) {
auto arr = info[0].As();
const auto N = arr.Length();
for (auto i = 0; j < N ; i++) {
MyClass::myVector[i].emplace_back(static_cast(arr[j]).ToString().Utf8Value());
}
return Napi::Boolean();
}
```
The code:
[Getting std::string from Napi::Array in C++](https://github.com/atom-community/zadeh/blob/3cade9d1ae2c719e76dc1acdc9ad0334c56ab0bf/src/binding/node_data_interface.h#L16)
[The loop for making vector of std::string](https://github.com/atom-community/zadeh/blob/3cade9d1ae2c719e76dc1acdc9ad0334c56ab0bf/src/ArrayFilterer.h#L40-L42)
[JS side](https://github.com/atom-community/zadeh/blob/3cade9d1ae2c719e76dc1acdc9ad0334c56ab0bf/src/binding/node.js#L37)
# The context:
The data comes in an array of JavaScript strings.
# The suggestions:
1) _Originally posted by @mhdawson in [this comment](https://github.com/nodejs/node-addon-api/issues/862#issuecomment-749064274)_
I made this suggestion in an issue a while back which I think addresses the same question: https://github.com/nodejs/node-addon-api/issues/429#issuecomment-457639332
If you need to more efficiently transfer an array between JS and Native, then Napi::ArrayBuffer may be better than using Napi::Array.
-----------------
2) _Originally posted by @NickNaso in [this comment](https://github.com/nodejs/node-addon-api/issues/862#issuecomment-749133934)_
we discussed this in the documentation about Napi::Array here https://github.com/nodejs/node-addon-api/blob/master/doc/array.md.
> `Napi::TypedArray` and `Napi::ArrayBuffer` correspond to JavaScript data types such as `Napi::Int32Array` and `Napi::ArrayBuffer`, respectively, that can be used for transferring large amounts of data from JavaScript to the native side. An example illustrating the use of a JavaScript-provided ArrayBuffer in native code is available [here](https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api).
In general, the conversion from JavaScript type to native type has a cost. In your code for example I can imagine that your JavaScript code is something like this:
```js
// ...
const NUMBER_OF_ELEMENTS = 1000
const arr = []
for (let i = 0; i
Contributor guide
Research direction
Start by reading the linked native conversion in src/binding/node_data_interface.h and the vector loop in src/ArrayFilterer.h, then compare the JavaScript entry point in src/binding/node.js with doc/array.md's ArrayBuffer guidance. Done means an agreed conversion approach for the JavaScript string array, with measurements showing whether it improves the reported 22ms cost.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, javascript, node.js
- Domain
- backend, performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100