v8 API Deep Dive

External Buffer Memory Example

  • v8 HeapSnapshot exposes an API to look at all nodes in C++ which is pretty quick
NAN_METHOD(Snapshot::RequestNodesByName) {
  Nan::HandleScope scope;

  // left out error handling

  Local<String> name = info[0].As<String>();
  Local<Function> fn = info[1].As<Function>();
  void* ptr = Nan::GetInternalFieldPointer(info.This(), 0);
  HeapSnapshot* self = static_cast<HeapSnapshot*>(ptr);

  for (SnapshotObjectId id = 0; id < self->GetMaxSnapshotJSObjectId(); id++) {
    const v8::HeapGraphNode* node = self->GetNodeById(id);
    if (node == nullptr) continue;
    if (name->StrictEquals(node->GetName())) {
      Local<Value> arg = GraphNode::New(node);
      fn->Call(Null(info.GetIsolate()), 1, &arg);
    }
  }
  return;
}