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@d04246a

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
@@ -780,13 +780,22 @@ function byteLength(string, encoding) {
780780
if (len === 0)
781781
return 0;
782782

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

792801
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.