package
Like `string.slice()` but bytewise
npm install string-byte-sliceGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
25.5 kB
Includes dependencies.
Like string.slice() but bytewise.
string.slice()
operates character-wise. Each character can take up to 4 bytes when serialized
with UTF-8 (in a file, network request, etc.). This library slices or truncates
a string to a specific amount of bytes instead.
string.slice()import stringByteSlice from 'string-byte-slice'
// Works like `string.slice()`
stringByteSlice('abcd', 1) // "bcd"
stringByteSlice('abcd', 1, 3) // "bc"
stringByteSlice('abcd', -3) // "bcd"
stringByteSlice('abcd', 0, -1) // "abc"
stringByteSlice('abcd', 0, 100) // "abcd"
stringByteSlice('abcd', 0, -100) // ""
// UTF-8 bytes length is taken into account
npm install string-byte-sliceThis package works in both Node.js >=18.18.0 and browsers.
This is an ES module. It must be loaded using
an import or import() statement,
not require(). If TypeScript is used, it must be configured to
output ES modules,
not CommonJS.
input string
start number
end number?
Return value: string
Returns a copy of input:
startend (excluded)start and end:
inputSince Unicode characters can span multiple bytes, if the first or last character
of the slice has been cut in its middle, it is discarded from the return value.
This means the start and end might end up being up to 3 bytes off from the
specified value.
There are several ways to slice a string bytewise:
string.charCodeAt()
or
string.codePointAt()Buffer, e.g.
Buffer.from()
and
buffer.subarray()Which one is the fastest depends on many factors including:
This library picks the fastest method based on the platform and the input.
String length
1 1e1 1e2 1e3 1e4 1e5 1e6 1e7 1e8
Small slice Only ASCII string-byte-slice 38.6ns 74.5ns 355ns 1.16μs 4.11μs 26.7μs 0.26ms 3.27ms 52.6ms
String.charCodeAt() 23.5ns 63.0ns 361ns 3.04μs 28.29μs 288.2μs 3.16ms 31.16ms 316.0ms
Buffer.from() 302.6ns 359.1ns 400ns 0.62μs 3.53μs 26.5μs 0.26ms 3.22ms 53.3ms
TextEncoder 975.4ns 975.9ns 989ns 1.04μs 1.49μs 17.6μs 0.20ms 2.65ms 33.7ms
Mostly ASCII string-byte-slice 38.8ns 68.0ns 392ns 2.33μs 9.96μs 97.8μs 0.95ms 9.97ms 103ms
Very few non-ASCII String.charCodeAt() 28.8ns 54.5ns 360ns 4.14μs 35.73μs 339.7μs 3.50ms 36.34ms 359ms
Buffer.from() 222.3ns 343.9ns 509ns 1.83μs 21.34μs 195.6μs 1.70ms 18.99ms 191ms
TextEncoder 809.5ns 1043.2ns 1105ns 1.95μs 11.49μs 95.8μs 0.95ms 10.53ms 103ms
Mostly ASCII string-byte-slice 38.8ns 48.1ns 235ns 3.48μs 16.6μs 206μs 2.02ms 20.7ms 214ms
Some non-ASCII String.charCodeAt() 28.8ns 35.9ns 233ns 2.11μs 22.3μs 241μs 2.01ms 26.5ms 212ms
Buffer.from() 222.3ns 252.8ns 624ns 3.30μs 34.6μs 342μs 3.36ms 34.7ms 368ms
TextEncoder 809.5ns 789.6ns 1156ns 2.95μs 20.1μs 205μs 2.02ms 20.8ms 224ms
Only non-ASCII string-byte-slice 38.6ns 55.1ns 202ns 1.93μs 16.6μs 154μs 1.38ms 16.7ms 167ms
String.charCodeAt() 23.5ns 50.6ns 185ns 1.42μs 17.2μs 166μs 1.68ms 16.7ms 168ms
string-byte-length: Get the
UTF-8 byte length of a stringtruncate-json: Truncate a JSON
stringFor any question, don't hesitate to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.
This project was made with ❤️. The simplest way to give back is by starring and sharing it online.
If the documentation is unclear or has a typo, please click on the page's Edit
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!
TextEncoder