Buffer in Node.js: Writing Data Methods

The Buffer class in Node.js provides a wide range of methods for writing data into binary memory. These methods cover signed and unsigned integers, floating-point numbers, big integers (bigint), and strings. They support both big-endian and little-endian formats, making them essential for binary file handling, network protocols, and low-level data operations.

buf.writeBigUInt64BE / buf.writeBigUInt64LEbuf.writeDoubleBE / buf.writeDoubleLEbuf.writeFloatBE / buf.writeFloatLEbuf.writeIntBE / buf.writeIntLEbuf.writeUIntBE / buf.writeUIntLE

~6 min read · Updated Dec 26, 2025

1. Writing BigUInt64


  • buf.writeBigUInt64BE(value, offset): Writes an unsigned 64-bit bigint in big-endian format.
  • buf.writeBigUInt64LE(value, offset): Writes an unsigned 64-bit bigint in little-endian format.
const buf = Buffer.allocUnsafe(8);
buf.writeBigUInt64BE(0xdecafafecacefaden, 0);
console.log(buf); // 

2. Writing Double and Float


  • buf.writeDoubleBE(value, offset): Writes a 64-bit floating-point number in big-endian format.
  • buf.writeDoubleLE(value, offset): Writes a 64-bit floating-point number in little-endian format.
  • buf.writeFloatBE(value, offset): Writes a 32-bit floating-point number in big-endian format.
  • buf.writeFloatLE(value, offset): Writes a 32-bit floating-point number in little-endian format.
const buf = Buffer.allocUnsafe(8);
buf.writeDoubleBE(123.456, 0);
console.log(buf);

3. Writing Small Integers


  • buf.writeInt8(value, offset): Writes a signed 8-bit integer.
  • buf.writeUInt8(value, offset): Writes an unsigned 8-bit integer.
const buf = Buffer.allocUnsafe(2);
buf.writeInt8(2, 0);
buf.writeInt8(-2, 1);
console.log(buf); // 

4. Writing Int16 and UInt16


  • buf.writeInt16BE(value, offset): Writes a signed 16-bit integer in big-endian format.
  • buf.writeInt16LE(value, offset): Writes a signed 16-bit integer in little-endian format.
  • buf.writeUInt16BE(value, offset): Writes an unsigned 16-bit integer in big-endian format.
  • buf.writeUInt16LE(value, offset): Writes an unsigned 16-bit integer in little-endian format.

5. Writing Int32 and UInt32


  • buf.writeInt32BE(value, offset): Writes a signed 32-bit integer in big-endian format.
  • buf.writeInt32LE(value, offset): Writes a signed 32-bit integer in little-endian format.
  • buf.writeUInt32BE(value, offset): Writes an unsigned 32-bit integer in big-endian format.
  • buf.writeUInt32LE(value, offset): Writes an unsigned 32-bit integer in little-endian format.

6. Writing IntBE / IntLE and UIntBE / UIntLE


Writes integers of variable length (1–6 bytes) in big-endian or little-endian format.

const buf = Buffer.allocUnsafe(6);
buf.writeIntBE(0x1234567890ab, 0, 6);
console.log(buf); // 

7. Writing Strings


buf.write(string[, offset[, length]][, encoding]): Writes a string into the Buffer using the specified encoding (default: UTF-8).

const buf = Buffer.alloc(256);
const len = buf.write('½ + ¼ = ¾', 0);
console.log(`${len} bytes: ${buf.toString('utf8',0,len)}`);

Conclusion


The Buffer write methods provide precise control over storing binary data in different formats. With support for signed and unsigned integers, floating-point numbers, big integers, and strings, these methods are essential for low-level data processing in Node.js applications.

1. Handling Function Arguments


Addons often expose functions to JavaScript. These functions must validate the number and type of arguments before performing operations.

// addon.cc
void Add(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();

  if (args.Length() < 2) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate,"Wrong number of arguments").ToLocalChecked()));
    return;
  }

  if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate,"Wrong arguments").ToLocalChecked()));
    return;
  }

  double value = args[0].As()->Value() + args[1].As()->Value();
  Local num = Number::New(isolate, value);
  args.GetReturnValue().Set(num);
}

This function adds two numbers and returns the result to JavaScript.

2. Executing Callbacks


Addons can accept JavaScript functions as arguments and invoke them from C++.

// addon.cc
void RunCallback(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();
  Local context = isolate->GetCurrentContext();
  Local cb = Local::Cast(args[0]);
  const unsigned argc = 1;
  Local argv[argc] = {
      String::NewFromUtf8(isolate,"hello world").ToLocalChecked() };
  cb->Call(context, Null(isolate), argc, argv).ToLocalChecked();
}

In this example, the callback is invoked synchronously and prints "hello world".

3. Object Factory


Addons can create and return new objects from C++ functions. These objects can contain properties set from input arguments.

// addon.cc
void CreateObject(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();
  Local context = isolate->GetCurrentContext();

  Local obj = Object::New(isolate);
  obj->Set(context,
           String::NewFromUtf8(isolate,"msg").ToLocalChecked(),
           args[0]->ToString(context).ToLocalChecked()).FromJust();

  args.GetReturnValue().Set(obj);
}


This function creates an object with a msg property equal to the input string.

Conclusion


Node.js Addons provide powerful tools for extending JavaScript with native C/C++ functionality. Proper handling of function arguments, executing callbacks, and creating objects are key techniques for building efficient and reliable Addons.

1. Function Factory


This pattern creates a JavaScript function that wraps a C++ function and returns it back to JavaScript.

// addon.cc
void MyFunction(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world").ToLocalChecked());
}

void CreateFunction(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();
  Local context = isolate->GetCurrentContext();
  Local tpl = FunctionTemplate::New(isolate, MyFunction);
  Local fn = tpl->GetFunction(context).ToLocalChecked();
  fn->SetName(String::NewFromUtf8(isolate, "theFunction").ToLocalChecked());
  args.GetReturnValue().Set(fn);
}

This code creates a JavaScript function that, when invoked, returns the string "hello world".

2. Wrapping C++ Objects


Using node::ObjectWrap, C++ classes can be wrapped so that new instances can be created with the JavaScript new operator.

// myobject.h
class MyObject : public node::ObjectWrap {
 public:
  static void Init(v8::Local exports);
 private:
  explicit MyObject(double value = 0);
  ~MyObject();
  static void New(const v8::FunctionCallbackInfo& args);
  static void PlusOne(const v8::FunctionCallbackInfo& args);
  double value_;
};

In myobject.cc, methods are implemented and added to the prototype:

NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);

The plusOne method increments the internal value of the object.

3. Factory for Wrapped Objects


Instead of explicitly using new in JavaScript, a factory method can be implemented in C++ to create new instances:

// addon.cc
void CreateObject(const FunctionCallbackInfo& args) {
  MyObject::NewInstance(args);
}

This approach allows objects to be created without directly calling new in JavaScript.

Conclusion


The Function Factory and ObjectWrap patterns are powerful techniques for Node.js Addon development. They enable developers to expose C++ functions and objects directly to JavaScript, bridging native performance with the flexibility of JavaScript applications.

1. Creating and Passing Objects


A createObject function is used to instantiate new C++ objects. The add function then accepts two MyObject instances, unwraps them, and sums their internal values.

// addon.cc
void Add(const FunctionCallbackInfo& args) {
  Isolate* isolate = args.GetIsolate();
  Local context = isolate->GetCurrentContext();

  MyObject* obj1 = node::ObjectWrap::Unwrap(
      args[0]->ToObject(context).ToLocalChecked());
  MyObject* obj2 = node::ObjectWrap::Unwrap(
      args[1]->ToObject(context).ToLocalChecked());

  double sum = obj1->value() + obj2->value();
  args.GetReturnValue().Set(Number::New(isolate, sum));
}

2. Accessing Internal Values


To expose the private value_ field of MyObject, a public accessor method is added:

// myobject.h
inline double value() const { return value_; }

3. Implementing MyObject


The MyObject class is implemented similarly to previous versions, but now supports unwrapping so its values can be used across different functions.

MyObject::MyObject(double value) : value_(value) {}
void MyObject::NewInstance(const FunctionCallbackInfo& args) {
  // Create a new instance and return it
}

4. Testing in JavaScript


After compiling the Addon, objects can be created and passed around between functions:

// test.js
const addon = require('./build/Release/addon');

const obj1 = addon.createObject(10);
const obj2 = addon.createObject(20);
const result = addon.add(obj1, obj2);

console.log(result); // 30

Conclusion


Using ObjectWrap::Unwrap, wrapped C++ objects can be passed around in JavaScript and reused across multiple functions. This technique enables advanced interaction between native code and JavaScript, making it essential for building complex Node.js Addons.

Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue