You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, i just want to ask a question. This is actualy not a bug, just a little question.
My C++ experiences is old, i forget lot of things, but i wanna try this awesome option to create native modules for nodejs, just for experience.
I started to read api docs, google and some other stuffs but i don't find optimal solution for get and set arrays in c++ functions.
Here my example:
#include<nan.h>usingnamespacev8;usingnamespacestd;voidSortJsArray(Local<Array> &jsArray)
{
vector<double> input;
for (unsignedint i = 0; i < jsArray->Length(); i++)
{
Handle<Value> val = jsArray->Get(i);
double numVal = val->NumberValue();
input.push_back(numVal);
}
bool swap = true;
while (swap)
{
swap = false;
for (size_t i = 0; i < input.size() - 1; i++)
{
if (input[i] > input[i + 1])
{
double tmp = input[i];
input[i] = input[i + 1];
input[i + 1] = tmp;
swap = true;
}
}
}
for (size_t i = 0; i < input.size() - 1; i++)
{
Nan::Set(jsArray, i, Nan::New<Number>(input[i]));
}
}
voidbubbleSort(const Nan::FunctionCallbackInfo<v8::Value> &info)
{
if (!info[0]->IsArray())
{
Nan::ThrowTypeError("Argument must be array");
return;
}
Local<Array> jsArray = Local<Array>::Cast(info[0]);
SortJsArray(jsArray);
info.GetReturnValue().Set(jsArray);
}
voidInit(v8::Local<v8::Object> exports)
{
exports->Set(Nan::New("sort").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(bubbleSort)->GetFunction());
}
NODE_MODULE(hello, Init)
I use this for benchmark a javascript based solution and a c++ based solution.
My results is the following:
Bubble sorting with c++ x 276,766 ops/sec ±0.65% (93 runs sampled)
Bubble sorting with js x 578,968 ops/sec ±1.18% (91 runs sampled)
fastest
Bubble sorting with js
So my question is this: How can i convert javascript arrays to vector and vector to javascript array.
Maybe exists a faster way instead of iterate over the array? Pls help me out.
Thank you :)
The text was updated successfully, but these errors were encountered:
As long as you are using plain arrays and not typed arrays you're probably never going to beat the JS code.
The machine code that V8 emits for your JS code can specialize and take shortcuts but every element Get/Set call in your C++ code goes through a lot of layers to implement the proper JS semantics.
You can probably shave off some overhead by sorting in-place and reusing number objects instead of calling Nan::New<Number>(...) but that's about it. Hope that helps.
Hi, i just want to ask a question. This is actualy not a bug, just a little question.
My C++ experiences is old, i forget lot of things, but i wanna try this awesome option to create native modules for nodejs, just for experience.
I started to read api docs, google and some other stuffs but i don't find optimal solution for get and set arrays in c++ functions.
Here my example:
I use this for benchmark a javascript based solution and a c++ based solution.
My results is the following:
Bubble sorting with c++ x 276,766 ops/sec ±0.65% (93 runs sampled)
Bubble sorting with js x 578,968 ops/sec ±1.18% (91 runs sampled)
fastest
Bubble sorting with js
So my question is this: How can i convert javascript arrays to vector and vector to javascript array.
Maybe exists a faster way instead of iterate over the array? Pls help me out.
Thank you :)
The text was updated successfully, but these errors were encountered: