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

オープン 初心者向け
#67,847 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@AyushCipher がすでに取り組んでいます。

2026年11月30日 から。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
86/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, markdown
領域
documentation

調査の方向性

doc/api/buffer.md から始め、既存の buf.toString() のドキュメントと比較します。次に、lib/internal/buffer.js の addBufferPrototypeMethods() を調査し、公開メソッド名を確認します。デフォルト値、切り詰め、範囲エラー、および一般には toString() を使用することが推奨される点を含め、7つの buf.<encoding>Slice() メソッドを文書化します。レンダリングされた Buffer API ドキュメントを確認し、文書化されたすべてのシグネチャを実装と照合して検証します。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
JavaScript
スター
124k
フォーク
38k
平均マージ
4日 2時間
マージ済み PR(30日)
266

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nodejs/node のほかの issue

nodejs/node の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。