Skip to content
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

Help needed with arrays #640

Closed
devmetal opened this issue Jan 16, 2017 · 1 comment
Closed

Help needed with arrays #640

devmetal opened this issue Jan 16, 2017 · 1 comment
Labels

Comments

@devmetal
Copy link

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>

using namespace v8;
using namespace std;

void SortJsArray(Local<Array> &jsArray)
{
  vector<double> input;

  for (unsigned int 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]));
  }
}

void bubbleSort(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);
}

void Init(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 :)

@bnoordhuis
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants