
Company News
Meet the Socket Team at RSAC and BSidesSF 2026
Join Socket for live demos, rooftop happy hours, and one-on-one meetings during BSidesSF and RSA 2026 in San Francisco.
piece-table
Advanced tools
A piece table is an efficient data structure for tracking edits to a text document. A detailed explanation can be found here.
A piece table consists of two buffers, the file buffer and the add buffer, and a
table of "pieces", or windows into those buffers (the piece table). Each piece consists
of a length, an offset, and a descriptor of which buffer the piece points to. The actual
text of the document is represented by the series of pieces in the piece table.
Initially, the file buffer consists of the full text of the document and the add
buffer is empty. Insertions involve appending the new text to the add buffer and
adding pieces to the piece table to reflect the addition. Deletion is performed simply
by removing and editing pieces from the piece table. This has the added benefit of
never losing data, allowing easy undo operations. Piece table operations are very
efficient because they only ever append to the buffers, so no mid-array insertions or
deletions are performed.
npm install --save piece-table
const PieceTable = require("piece-table");
const document = new PieceTable("This is a document with some text.");
document.insert("This is some more text to insert at offset 33.", 33);
// Delete the previously inserted sentence
document.delete(79, 46);
var sequence = document.getSequence();
// sequence == "This is a document with some text."
var subString = document.stringAt(9, 8);
// subString == "document"
// PieceTable is an iterable:
for (let character of document) {
console.log(character);
// 'T', 'h', 'i', 's', ...
}
Kind: global class
A piece table is an efficient data structure to track changes to a text. See https://www.cs.unm.edu/~crowley/papers/sds/node15.html for details
| Param | Type | Description |
|---|---|---|
| fileText | string | the initial text of the piece table |
Inserts a string into the piece table
Kind: instance method of PieceTable
| Param | Type | Description |
|---|---|---|
| str | string | the string to insert |
| offset | number | the offset at which to insert the string |
Deletes a string from the piece table
Kind: instance method of PieceTable
| Param | Type | Description |
|---|---|---|
| offset | number | the offset at which to begin deletion |
| length | number | the number of characters to delete. If negative, deletes backwards |
stringGets the sequence as a string
Kind: instance method of PieceTable
Returns: string - The sequence
Gets a string of a particular length from the piece table at a particular offset
Kind: instance method of PieceTable
| Param | Type | Description |
|---|---|---|
| offset | number | the offset from which to get the string |
| length | number | the number of characters to return from the offset. If negative, looks backwards |
FAQs
An implementation of the piece table data structure
We found that piece-table demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Join Socket for live demos, rooftop happy hours, and one-on-one meetings during BSidesSF and RSA 2026 in San Francisco.

Research
/Security News
Malicious Packagist packages disguised as Laravel utilities install an encrypted PHP RAT via Composer dependencies, enabling remote access and C2 callbacks.

Research
/Security News
OpenVSX releases of Aqua Trivy 1.8.12 and 1.8.13 contained injected natural-language prompts that abuse local AI coding agents for system inspection and potential data exfiltration.