doc: document the Buffer.prototype.*Slice() methods

Offen Anfängerfreundlich
#67,847 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@AyushCipher arbeitet bereits daran.

Seit 30.11.2026.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
86/100
Issue-Typ
Dokumentation
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
javascript, markdown
Bereich
documentation

Rechercherichtung

Beginne mit doc/api/buffer.md und vergleiche die vorhandene Dokumentation zu buf.toString(). Untersuche anschließend addBufferPrototypeMethods() in lib/internal/buffer.js, um die Namen der öffentlichen Methoden zu bestätigen. Dokumentiere die sieben Methoden buf.<encoding>Slice() einschließlich Standardwerten, Kürzung, Bereichsfehlern und der Empfehlung, grundsätzlich toString() zu verwenden. Prüfe die gerenderte Buffer-API-Dokumentation und verifiziere jede dokumentierte Signatur gegen die Implementierung.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

doc
Affected URL(s)

https://nodejs.org/api/buffer.html

Description of the problem

Buffer.prototype has seven encoding-specific methods that are public, supported in practice, and undocumented: asciiSlice(), base64Slice(), base64urlSlice(), hexSlice(), latin1Slice(), ucs2Slice() and utf8Slice(). addBufferPrototypeMethods() in lib/internal/buffer.js attaches them. They are not mentioned in doc/api/buffer.md on main, v24.x or v26.x.

They are used in the ecosystem because they decode a range without the work buf.toString(encoding, start, end) does first: clamping start/end with MathTrunc() and looking up the encoding with getEncodingOps(). Both end up in the same native functions. For short strings the difference is significant. For example, a tar header parser decodes several strings of 10 to 100 bytes per entry, and buf.utf8Slice(start, end) takes 122 ns per 20-byte string against 141 to 165 ns for buf.toString('utf8', start, end) (Node.js 24; 136 against 156 ns on 26.13.1).

#46467 raised this. Commenters favoured documenting them over leaving them public and undocumented, but it was closed as stale without a change.

I propose documenting them, for example as one section, buf.<encoding>Slice([start[, end]]), that says:

  • they decode buf[start, end) with the given encoding, like buf.toString(encoding, start, end), without looking up the encoding;
  • start defaults to 0 and end to buf.length, and fractional values are truncated;
  • unlike toString(), indexes outside of the buffer are not clamped: they throw ERR_OUT_OF_RANGE (Buffer.from('abc').utf8Slice(0, 100)), while start > end returns '';
  • toString() remains the recommended general API, and these are for when the encoding is known in advance.

I measured this behaviour on Node.js 24.22.3 and 26.13.1, and it is the same on both.

The matching *Write() methods (utf8Write(), latin1Write(), …) are attached and undocumented in the same way, and could be documented together, as was suggested in #46467.

I'm happy to send the docs PR.

Vorherrschende Sprache
JavaScript
Sterne
124k
Forks
38k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
266

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus nodejs/node

Alle Issues in nodejs/node

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.