GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: optimize byteLength for common encodings · nodejs/node@28ca678

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • buffer.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+15-6Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,22 @@ function byteLength(string, encoding) {
782782
if (len === 0)
783783
return 0;
784784

785-
if (encoding) {
786-
const ops = getEncodingOps(encoding);
787-
if (ops) {
788-
return ops.byteLength(string);
789-
}
785+
if (!encoding || encoding === 'utf8') {
786+
return byteLengthUtf8(string);
787+
}
788+
789+
if (encoding === 'ascii') {
790+
return len;
790791
}
791-
return byteLengthUtf8(string);
792+
793+
const ops = getEncodingOps(encoding);
794+
if (ops === undefined) {
795+
// TODO (ronag): Makes more sense to throw here.
796+
// throw new ERR_UNKNOWN_ENCODING(encoding);
797+
return byteLengthUtf8(string);
798+
}
799+
800+
return ops.byteLength(string);
792801
}
793802

794803
Buffer.byteLength = byteLength;

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.