title: NodeJS: Why nodejs c++ addon is so slow?
body: <p>I'm trying to write a very simple nodejs c++ addon and compare its usage with usual nodejs module usage (written on CoffeeScript). I wrote a test that creates a million object instances of AddonClass from addon and CoffeeClass from module. The source code is:</p>
  
  <p>addon.cpp</p>
  
  <pre><code>#define BUILDING_NODE_EXTENSION
  #include &lt;node.h&gt;
  #include "addon_class.hpp"
  using namespace v8;
  static void InitAll(Handle&lt;Object&gt; target) {
    Addon::Init(target);
  }
  NODE_MODULE(addon, InitAll)
  </code></pre>
  
  <p>addon_class.hpp</p>
  
  <pre><code>#ifndef _ADDON_CLASS_HPP_
  #define _ADDON_CLASS_HPP_
  
  #include &lt;node.h&gt;
  
  class Addon : public node::ObjectWrap {
   public:
    static void Init(v8::Handle&lt;v8::Object&gt; target);
   private:
    Addon(int value) : value_(value) {};
    ~Addon() {};
    static v8::Handle&lt;v8::Value&gt; New(const v8::Arguments&amp; args);
    int value_;
  };
  
  #endif
  </code></pre>
  
  <p>addon_class.cpp</p>
  
  <pre><code>#define BUILDING_NODE_EXTENSION
  #include &lt;cstdio&gt;
  #include &lt;node.h&gt;
  #include "addon_class.hpp"
  using namespace v8;
  
  Persistent&lt;FunctionTemplate&gt; tpl;
  
  void Addon::Init(Handle&lt;Object&gt; target) {
    Local&lt;FunctionTemplate&gt; t = FunctionTemplate::New(New);
    tpl = Persistent&lt;FunctionTemplate&gt;::New(t);
    tpl-&gt;SetClassName(String::NewSymbol("AddonClass"));
    tpl-&gt;InstanceTemplate()-&gt;SetInternalFieldCount(1);
    target-&gt;Set(String::NewSymbol("AddonClass"), tpl-&gt;GetFunction());
  }
  
  Handle&lt;Value&gt; Addon::New(const Arguments&amp; args) {
    HandleScope scope;
    Addon* obj = new Addon(0);
    if (args[0]-&gt;IsNumber()) {
      obj-&gt;value_ = args[0]-&gt;NumberValue();
    }
    obj-&gt;Wrap(args.This());
    return args.This();
  }
  </code></pre>
  
  <p>CoffeeClass.coffee</p>
  
  <pre><code>class CoffeeClass
    value: undefined
  
    constructor: (@value) -&gt;
      if !@value? then @value = 0
  
  exports.CoffeeClass = CoffeeClass 
  </code></pre>
  
  <p>benchmark.coffee</p>
  
  <pre><code>CoffeeClass = (require './CoffeeClass.coffee').CoffeeClass
  AddonClass = (require './build/Release/addon.node').AddonClass
  
  N = 1e6
  
  calculateDiff = (d1, d2) -&gt;
    h = parseInt d2.getHours()
    h -= parseInt d1.getHours()
    m = parseInt d2.getMinutes() + h * 60
    m -= parseInt d1.getMinutes()
    s = parseInt d2.getSeconds() + m * 60
    s -= parseInt d1.getSeconds()
    ms = parseInt d2.getMilliseconds() + s * 1000
    ms -= parseInt d1.getMilliseconds()
    return ms
  
  testCreate = (LC) -&gt;
    d1 = new Date()
    for i in [1..N]
      l = new LC
    d2 = new Date()
    console.log LC.name, calculateDiff(d1, d2), "ms"
  
  testCreate(CoffeeClass)
  testCreate(AddonClass)
  </code></pre>
  
  <p>The result of this benchmark is very, very strange for me:</p>
  
  <pre><code>CoffeeClass 34 ms
  AddonClass 487 ms
  </code></pre>
  
  <p>So, the question is: why is this simple addon so slow? And, more importantly, is it possible to do something with it or not?</p>