Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save trevnorris/0e9db826789dda8b837e to your computer and use it in GitHub Desktop.

Select an option

Save trevnorris/0e9db826789dda8b837e to your computer and use it in GitHub Desktop.
Compiles against io.js v2.0
{
"targets": [{
"target_name": "addon",
"sources": [ "main.cc" ]
}]
}
#include <v8.h>
#include <node.h>
using namespace v8;
void RunMe(const FunctionCallbackInfo<Value>& args) {
uint32_t l = args[0]->Uint32Value();
Local<ArrayBuffer> ab = ArrayBuffer::New(args.GetIsolate(), l);
Local<Uint8Array> u8 = Uint8Array::New(ab, 0, l);
u8->SetPrototype(args[1]);
args.GetReturnValue().Set(u8);
}
void init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "runMe", RunMe);
}
NODE_MODULE(addon, init)
'use strict';
const addon = require('./build/Release/addon');
const runMe = addon.runMe;
const Buffer = require('buffer').Buffer;
const bp = Buffer.prototype;
Buffer.__proto__ = Uint8Array;
Buffer.prototype.__proto__ = Uint8Array.prototype;
function Nuffer(n) {
return runMe(n, bp);
}
Nuffer.__proto__ = Buffer;
Nuffer.prototype.__proto__ = Buffer.prototype;
/*
var n = new Nuffer(5);
console.log(n, n.length);
/* */
var t = process.hrtime();
for (var i = 0; i < 1e7; i++) {
new Nuffer(1);
}
t = process.hrtime(t);
t = t[0] * 1e9 + t[1];
console.log((t / 1e7).toFixed(1) + ' ns/op');
/* */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment