So I'm pretty confused about how to use Arrays/TypedArrays using this library. Basically, I'm looking for the equivalent of the .push method. I'd also love a way to reserve space in chunks, as I know how big the resulting array is going to be, though that might be too complicated considering V8.
The documentation includes a .Set(int, Value) with the example obj.Set(42, "The Answer to Life, the Universe, and Everything");. This example is given for Object, not for Array (I can't find anything with Array at all).
I tried to do something similar in the code (I'm trying to make an array of arrays):
obj.Set(0, Napi::Array::New(env));
but got the error:
error: call of overloaded ‘Set(int, Napi::Array)’ is ambiguous
obj.Set(0, Napi::Array::New(env));
I'm very confused as to why that's ambiguous, but I also suspect it won't update the length properly or at least will be quite inefficient. When I wrap 0 in uint32_t(0), it compiles. Either way, the documentation here is pretty lacking and I think an example using arrays efficiently would be very welcome.
Hi @rivertam,
sorry for the late in answer. As you suggested in the next days I will try to add some examples about Napi::Array and improve the corresponding documentation.
Great, thanks @NickNaso!
I did try the .Set method but this turned out to be WAY slower (10+ seconds in my case) than just encoding the array as a buffer and then looping through the bytes in an orderly fashion in JS (30-50ms).
Hi @NickNaso @rivertam,
I am also facing the problem with arrays. I already have a C++ implemented library which accepts an integer array.
but "node-addon-api" have Napi::Array. I didn't find the conversion method for this.
if I create new int array then assign all element from Napi::Array will be expensive. so what do you suggest?
@abhi11210646 converting from a JavaScript array to a native array is an expensive operation. OTOH you can use a JavaScript Int32Array as in the following example: https://github.com/gabrielschulhof/node-addon-examples/blob/array_buffer_to_native/array_buffer_to_native/node-addon-api/index.js. On the native side, you can accept the resulting ArrayBuffer and pass the data directly to the native side without needing to convert from a JavaScript type to a native type. BTW, I've submitted the above example to node-addon-examples.
Landed the example in node-addon-examples. Let is know if that resolves your questions.
Hi @gabrielschulhof, it worked this way. thank you.
For those (like myself) who want a link to the example @mhdawson is referencing: https://github.com/nodejs/node-addon-examples/commit/7124e5f34d98a3a64e2eb2d2500eaf7cb4be8457#diff-36c91849c5180ea4227b8c709138a26b
@rivertam I want update you that the example has been landed in the example repo and eveyone can find it here: https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api and @gabrielschulhof added some explanations in the array documentation https://github.com/nodejs/node-addon-api/pull/465
What do you think? Do we need to do more?
Yeah, I think this is exactly what I was looking for at the time. I can't test it or anything because we essentially dropped that project (we had to move to Python for unrelated reasons -- I'm still bitter), but this definitely seems like it covers the concern I had initially.
I suppose I'll close this, but I'm not sure what the exact protocol here is so anyone can feel free to reopen.
Most helpful comment
Hi @rivertam,
sorry for the late in answer. As you suggested in the next days I will try to add some examples about
Napi::Arrayand improve the corresponding documentation.