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

Aperta Adatta ai principianti
#67,847 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@AyushCipher ci sta già lavorando.

Dal 30/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
86/100
Tipo di issue
Documentazione
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, markdown
Ambito
documentation

Direzione di ricerca

Inizia da doc/api/buffer.md e confronta la documentazione esistente di buf.toString(). Quindi esamina addBufferPrototypeMethods() in lib/internal/buffer.js per confermare i nomi dei metodi pubblici. Documenta i sette metodi buf.<encoding>Slice(), inclusi valori predefiniti, troncamento, errori di intervallo e raccomandazione di usare generalmente toString(). Controlla la documentazione dell’API Buffer generata e verifica ogni firma documentata rispetto all’implementazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
JavaScript
Stelle
124k
Fork
38k
Merge medio
4g 2h
PR unite (30g)
266

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nodejs/node

Tutte le issue di nodejs/node

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.