@vltpkg/fast-split 1.3.8 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -17
- package/dist/index.d.ts +1 -1
- package/dist/index.js +34 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,25 +9,32 @@ to quickly parse a small-to-medium sized string by a given delimiter.
|
|
|
9
9
|
|
|
10
10
|
## How Fast Is It!?
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
delimiter.
|
|
14
|
-
|
|
12
|
+
About 1.5x faster than `str.split()` for splitting short strings by a
|
|
13
|
+
short delimiter, about 1.1-1.3x when walking or transforming the
|
|
14
|
+
resulting list, and about 1.9x when limiting the result to 2 or 3
|
|
15
|
+
parts (a dedicated fast path, see `src/index.ts`).
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
Note: V8 caches `split()` results (no limit) for internalized strings
|
|
18
|
+
(literals, JSON keys, short JSON values), so splitting the same such
|
|
19
|
+
string repeatedly, as benchmarks over literals do, favors native
|
|
20
|
+
`split()`. Substrings, regex captures and built strings don't hit that
|
|
21
|
+
cache, and with many distinct internalized strings the cache is a net
|
|
22
|
+
cost.
|
|
23
|
+
|
|
24
|
+
Linux x64, node 26.11.1. Counts are operations per ms, splitting the
|
|
25
|
+
string '1.2.3-asdf+foo' (built at runtime) by the delimiter '.',
|
|
26
|
+
transforms calling part.toUpperCase(), and limits at 2 items
|
|
20
27
|
|
|
21
28
|
```
|
|
22
|
-
split
|
|
23
|
-
fastSplit
|
|
24
|
-
splitEmptyCheck
|
|
25
|
-
fastSplitEmptyCheck
|
|
26
|
-
splitTransformLoop
|
|
27
|
-
splitTransformMap
|
|
28
|
-
fastSplitTransform
|
|
29
|
-
splitLimit
|
|
30
|
-
fastSplitLimit
|
|
29
|
+
split 4339.624
|
|
30
|
+
fastSplit 6849.283
|
|
31
|
+
splitEmptyCheck 4190.307
|
|
32
|
+
fastSplitEmptyCheck 4803.402
|
|
33
|
+
splitTransformLoop 2772.453
|
|
34
|
+
splitTransformMap 2904.390
|
|
35
|
+
fastSplitTransform 3465.690
|
|
36
|
+
splitLimit 5491.101
|
|
37
|
+
fastSplitLimit 10384.471
|
|
31
38
|
```
|
|
32
39
|
|
|
33
40
|
## Usage
|
|
@@ -54,6 +61,6 @@ const [first, second, rest] = fastSplit(str, '.', 3)
|
|
|
54
61
|
// than getting an array and subsequently looping over it
|
|
55
62
|
// pass `-1` as the limit to give us all parts
|
|
56
63
|
const nums = fastSplit(str, '.', -1, (part, parts, index) =>
|
|
57
|
-
Number(
|
|
64
|
+
Number(part),
|
|
58
65
|
)
|
|
59
66
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ export type VoidReplace<T, R = string> = undefined extends T ? NullReplace<Exclu
|
|
|
36
36
|
* // providing the onPart method, which will be slightly faster
|
|
37
37
|
* // than getting an array and subsequently looping over it
|
|
38
38
|
* // pass `-1` as the limit to give us all parts
|
|
39
|
-
* const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(
|
|
39
|
+
* const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(part))
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
42
|
export declare function fastSplit<T = string>(str: string, delim: string, limit: number, onPart: (part: string, parts: NullToString<T>[], i: number) => T): NullToString<T>[];
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,38 @@
|
|
|
1
|
+
// A `limit` of 2 or 3 with no `onPart` is the "split off a prefix" case
|
|
2
|
+
// (`a b` => [a, b], `M.m.p-rest` => [M, m, p-rest]). These build the
|
|
3
|
+
// result as a fixed-shape array literal, which V8 allocates exactly
|
|
4
|
+
// sized, where the generic loop's push-grown array starts with a 17-slot
|
|
5
|
+
// backing store: that allocation is most of the cost for such short
|
|
6
|
+
// results. They are separate functions on purpose: adding this code
|
|
7
|
+
// inline pushes fastSplit past V8's bytecode budget for inlining it into
|
|
8
|
+
// callers (~460 bytes), which made the generic path ~30% slower.
|
|
9
|
+
const splitTwo = (str, delim) => {
|
|
10
|
+
const a = str.indexOf(delim);
|
|
11
|
+
return a === -1 ?
|
|
12
|
+
[str]
|
|
13
|
+
: [str.substring(0, a), str.substring(a + delim.length)];
|
|
14
|
+
};
|
|
15
|
+
const splitThree = (str, delim) => {
|
|
16
|
+
const a = str.indexOf(delim);
|
|
17
|
+
if (a === -1)
|
|
18
|
+
return [str];
|
|
19
|
+
const l = delim.length;
|
|
20
|
+
const b = str.indexOf(delim, a + l);
|
|
21
|
+
return b === -1 ?
|
|
22
|
+
[str.substring(0, a), str.substring(a + l)]
|
|
23
|
+
: [
|
|
24
|
+
str.substring(0, a),
|
|
25
|
+
str.substring(a + l, b),
|
|
26
|
+
str.substring(b + l),
|
|
27
|
+
];
|
|
28
|
+
};
|
|
1
29
|
export function fastSplit(str, delim, limit = -1, onPart) {
|
|
30
|
+
if (onPart === undefined) {
|
|
31
|
+
if (limit === 2)
|
|
32
|
+
return splitTwo(str, delim);
|
|
33
|
+
if (limit === 3)
|
|
34
|
+
return splitThree(str, delim);
|
|
35
|
+
}
|
|
2
36
|
let i = 0;
|
|
3
37
|
let p = 0;
|
|
4
38
|
const l = delim.length;
|
package/package.json
CHANGED