Skip to content

Replace indexOf === 0 with startsWith#1956

Merged
ai merged 1 commit intopostcss:mainfrom
Juneezee:refactor/replace-indexOf-with-startsWith
Aug 6, 2024
Merged

Replace indexOf === 0 with startsWith#1956
ai merged 1 commit intopostcss:mainfrom
Juneezee:refactor/replace-indexOf-with-startsWith

Conversation

@Juneezee
Copy link
Copy Markdown
Contributor

@Juneezee Juneezee commented Aug 6, 2024

A small PR.

It is more readable to call startsWith than to call indexOf and compare the result with zero to determine whether a string starts with a given prefix.

startsWith is also more efficient as it only compares at the beginning of the string, while indexOf searches the entire string.

It is more readable to call `startsWith` than to call `indexOf` and
compare the result with zero to determine whether a string starts with a
given prefix.

`startsWith` is also more efficient as it only compares at the beginning
of the string, while `indexOf` searches the entire string.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@ai
Copy link
Copy Markdown
Member

ai commented Aug 6, 2024

Can you run https://github.com/postcss/benchmark before and after the change?

@Juneezee
Copy link
Copy Markdown
Contributor Author

Juneezee commented Aug 6, 2024

Before:

pnpm test parsers

> @ test /home/jun/Desktop/github/benchmark
> eslint . && gulp "parsers"

[22:21:34] Using gulpfile ~/Desktop/github/benchmark/gulpfile.js
[22:21:34] Starting 'parsers'...
[22:21:34] Starting 'bootstrap'...
[22:21:34] Finished 'bootstrap' after 3.7 ms
[22:21:34] Starting '<anonymous>'...
[22:21:34] Running suite Parsers [/home/jun/Desktop/github/benchmark/parsers.js]...
[22:21:40]    Rework x 12.01 ops/sec ±1.70% (35 runs sampled)
[22:21:46]    Next PostCSS x 19.77 ops/sec ±7.35% (54 runs sampled)
[22:21:52]    PostCSS x 19.88 ops/sec ±8.45% (55 runs sampled)
[22:21:58]    PostCSS Full x 7.39 ops/sec ±7.63% (33 runs sampled)
[22:22:04]    CSSOM x 25.10 ops/sec ±7.48% (48 runs sampled)
[22:22:09]    Mensch x 15.63 ops/sec ±7.72% (46 runs sampled)
[22:22:17]    Gonzales x 3.76 ops/sec ±4.26% (14 runs sampled)
[22:22:23]    CSSTree x 15.88 ops/sec ±5.30% (45 runs sampled)
[22:22:30]    ParserLib x 3.81 ops/sec ±1.90% (14 runs sampled)
[22:22:37]    Stylecow x 8.14 ops/sec ±5.30% (25 runs sampled)
[22:22:42]    Stylis x 47.36 ops/sec ±2.29% (64 runs sampled)
[22:22:42] Fastest test is Stylis at 1.89x faster than CSSOM

Stylis:       21 ms  (2.4 times faster)
CSSOM:        40 ms  (1.3 times faster)
PostCSS:      50 ms
Next PostCSS: 51 ms  (1.0 times slower)
CSSTree:      63 ms  (1.3 times slower)
Mensch:       64 ms  (1.3 times slower)
Rework:       83 ms  (1.7 times slower)
Stylecow:     123 ms (2.4 times slower)
PostCSS Full: 135 ms (2.7 times slower)
ParserLib:    263 ms (5.2 times slower)
Gonzales:     266 ms (5.3 times slower)

[22:22:42] Finished '<anonymous>' after 1.13 min
[22:22:42] Finished 'parsers' after 1.13 min

After:

I updated package.json to use my local postcss by "postcss": "file:../postcss"

pnpm test parsers

> @ test /home/jun/Desktop/github/benchmark
> eslint . && gulp "parsers"

[22:23:42] Using gulpfile ~/Desktop/github/benchmark/gulpfile.js
[22:23:42] Starting 'parsers'...
[22:23:42] Starting 'bootstrap'...
[22:23:42] Finished 'bootstrap' after 3.69 ms
[22:23:42] Starting '<anonymous>'...
[22:23:43] Running suite Parsers [/home/jun/Desktop/github/benchmark/parsers.js]...
[22:23:49]    Rework x 12.09 ops/sec ±2.44% (35 runs sampled)
[22:23:54]    Next PostCSS x 20.58 ops/sec ±7.20% (56 runs sampled)
[22:24:00]    PostCSS x 19.58 ops/sec ±8.67% (54 runs sampled)
[22:24:07]    PostCSS Full x 7.50 ops/sec ±6.63% (34 runs sampled)
[22:24:12]    CSSOM x 25.09 ops/sec ±4.69% (47 runs sampled)
[22:24:18]    Mensch x 16.14 ops/sec ±8.19% (47 runs sampled)
[22:24:25]    Gonzales x 3.91 ops/sec ±4.09% (14 runs sampled)
[22:24:31]    CSSTree x 16.46 ops/sec ±5.26% (47 runs sampled)
[22:24:39]    ParserLib x 3.76 ops/sec ±2.57% (14 runs sampled)
[22:24:45]    Stylecow x 8.31 ops/sec ±6.19% (26 runs sampled)
[22:24:51]    Stylis x 46.37 ops/sec ±8.02% (64 runs sampled)
[22:24:51] Fastest test is Stylis at 1.85x faster than CSSOM

Stylis:       22 ms  (2.4 times faster)
CSSOM:        40 ms  (1.3 times faster)
Next PostCSS: 49 ms  (1.1 times faster)
PostCSS:      51 ms
CSSTree:      61 ms  (1.2 times slower)
Mensch:       62 ms  (1.2 times slower)
Rework:       83 ms  (1.6 times slower)
Stylecow:     120 ms (2.4 times slower)
PostCSS Full: 133 ms (2.6 times slower)
Gonzales:     256 ms (5.0 times slower)
ParserLib:    266 ms (5.2 times slower)

[22:24:51] Finished '<anonymous>' after 1.13 min
[22:24:51] Finished 'parsers' after 1.13 min

@ai ai merged commit ea262f3 into postcss:main Aug 6, 2024
@ai
Copy link
Copy Markdown
Member

ai commented Aug 6, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants