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

Ouverte Adaptée aux débutants
#67,847 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@AyushCipher y travaille déjà.

Depuis le 30/11/2026.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
86/100
Type d'issue
Documentation
Clarté
Clairement spécifiée
Activité
Active
Stack technique
javascript, markdown
Domaine
documentation

Piste de recherche

Commencez par doc/api/buffer.md et comparez la documentation existante de buf.toString(). Inspectez ensuite addBufferPrototypeMethods() dans lib/internal/buffer.js afin de confirmer les noms des méthodes publiques. Documentez les sept méthodes buf.<encoding>Slice(), notamment les valeurs par défaut, la troncature, les erreurs de plage et la recommandation d’utiliser généralement toString(). Vérifiez la documentation de l’API Buffer rendue et confirmez que chaque signature documentée correspond à l’implémentation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
JavaScript
Étoiles
124k
Forks
38k
Merge moyen
4 j 2 h
PR mergées (30 j)
266

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de nodejs/node

Toutes les issues de nodejs/node

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.