<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj" -->

---
title: Be aware of Node.js Buffer shared memory pool | daily.dev
description: Node.js `Buffer` objects share a memory pool for small memory spaces (under 8kb). This can lead to unexpected data exposure, as demonstrated by creating two...
canonical: https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Be aware of Node.js Buffer shared memory pool | daily.dev
og:description: Node.js `Buffer` objects share a memory pool for small memory spaces (under 8kb). This can lead to unexpected data exposure, as demonstrated by creating two...
og:url: https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj
og:image: https://api.daily.dev/og/posts/92QudRXcj.png
og:image:alt: Be aware of Node.js Buffer shared memory pool
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Be aware of Node.js Buffer shared memory pool

**[Node.js developers](https://daily.dev/sources/nodejsdevelopers)** · [@pavl_ro](https://daily.dev/pavl_ro) · 1 min read · 18 upvotes · 2 comments

## Summary

Node.js `Buffer` objects share a memory pool for small memory spaces (under 8kb). This can lead to unexpected data exposure, as demonstrated by creating two buffers that reveal a private key through another buffer instance. It's crucial to be aware of this behavior to avoid security risks.

## Content

If you're working with Node.js `Buffer` be aware that it has a shared memory pool.

When you create 2 buffers that occupy small memory space (under 8kb), their content is placed in the same memory pool.

```javascript
// Some private key information
const privateKey = Buffer.from('private key');
const hackyBuffer = Buffer.from('1');

const typedArray = new Uint8Array(hackyBuffer.buffer)
  .filter(value => value !== 0);
const charCodes = Array.from(typedArray);

// Prints "//private key1"
console.log(String.fromCodePoint(...charCodes));
```

Notice that we're not referencing the `privateKey` buffer in any way after its declaration and are still able to get its content via the buffer that we created elsewhere in the program.

## Community discussion

Top comments from developers on daily.dev.

**@hri7566** · 4 upvotes

> this might be obvious info, but i wanted to point out that this happens in Node.js and when using `import { Buffer } from "node:buffer";` in Deno, but Bun does not have this problem

---

Tags: [#security](https://daily.dev/tags/security), [#javascript](https://daily.dev/tags/javascript), [#nodejs](https://daily.dev/tags/nodejs)

[View this post on daily.dev](https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj","headline":"Be aware of Node.js Buffer shared memory pool","text":"Node.js `Buffer` objects share a memory pool for small memory spaces (under 8kb). This can lead to unexpected data exposure, as demonstrated by creating two buffers that reveal a private key through another buffer instance. It's crucial to be aware of this behavior to avoid security risks.","url":"https://daily.dev/posts/be-aware-of-node-js-buffer-shared-memory-pool-92qudrxcj","datePublished":"2024-08-05T02:01:08.812Z","dateModified":"2024-08-05T02:01:17.976Z","author":{"@type":"Person","name":"Pavel Romanov","url":"https://daily.dev/pavl_ro","image":"https://media.daily.dev/image/upload/s--6Q2lnUNa--/f_auto/v1715581987/avatars/avatar_gKJMx9kKXdRpYViAnTF4H","description":"Software engineer and technical writer.\n\nInterested in JavaScript and the ecosystem around it.","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":2675}},"interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":18},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"comment":[{"@type":"Comment","text":"this might be obvious info, but i wanted to point out that this happens in Node.js and when using import { Buffer } from \u0026quot;node:buffer\u0026quot;; in Deno, but Bun does not have this problem","datePublished":"2024-08-05T03:23:17.992Z","url":"https://daily.dev/posts/92QudRXcj#c-cM0KcoM8m","author":{"@type":"Person","name":"Hri7566","url":"https://daily.dev/hri7566","image":"https://avatars.githubusercontent.com/u/18133844?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":4}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/nodejsdevelopers","name":"Node.js developers"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Node.js developers","item":"https://daily.dev/squads/nodejsdevelopers"},{"@type":"ListItem","position":3,"name":"Be aware of Node.js Buffer shared memory pool"}]}
```

