Skip to content

chore: speed up space-only string check in lib/parser.js#2064

Merged
ai merged 2 commits intopostcss:mainfrom
hyperz111:blank-check
Dec 31, 2025
Merged

chore: speed up space-only string check in lib/parser.js#2064
ai merged 2 commits intopostcss:mainfrom
hyperz111:blank-check

Conversation

@hyperz111
Copy link
Copy Markdown
Contributor

This code snippet:

!str.trim();

is faster than:

/^\s*$/.test(str);

@ai
Copy link
Copy Markdown
Member

ai commented Dec 30, 2025

Interesting.

Can you run https://github.com/postcss/benchmark to see the difference?

@hyperz111
Copy link
Copy Markdown
Contributor Author

Interesting.

Can you run https://github.com/postcss/benchmark to see the difference?

I cannot run it. But you can try this.

import Benchmark from "benchmark";

const suite = new Benchmark.Suite();
const name = " \t"

suite
        .add("regex", function () {
                /^\s*$/.test(name);
        })
        .add("!trim", function () {
                !name.trim();
        })
        .on("cycle", function (event) {
                console.log(String(event.target));
        })
        .on("complete", function () {
                console.log("Fastest is " + this.filter("fastest").map("name"));
        })
        .run({ async: true });

@ai
Copy link
Copy Markdown
Member

ai commented Dec 30, 2025

I cannot run it.

What error do you have? Let’s fix it.

you can try this.

Isolated benchmarks will not tell how it affects overall performance.

Sometimes better isolated results could be worse for overall performance (for instance, because of memory consumptions, etc).

It optimizations it is critical rule to always run real use case benchmark.

@hyperz111
Copy link
Copy Markdown
Contributor Author

hyperz111 commented Dec 31, 2025

What error do you have? Let’s fix it.

My device storage, i using a phone with 32GB storage with ±100mb left. But i have try my own benchmark for this.

Code:

import Benchmark from "benchmark";
import Input from "postcss/lib/input";
import Parser from "postcss/lib/parser";
import ParserNew from "../../gitclone/postcss/lib/parser.js";
import { readFileSync } from "fs";

const suite = new Benchmark.Suite();
const file = readFileSync("../../downloads/bootstrap.css", "utf8");
const input = new Input(file);

suite
        .add("Current", function () {
                new Parser(input).parse();
        })
        .add("New", function () {
                new ParserNew(input).parse();
        })
        .on("cycle", function (event) {
                console.log(String(event.target));
        })
        .on("complete", function () {
                console.log("Fastest is " + this.filter("fastest").map("name"));
        })
        .run({ async: true });

Result:

$ node postcss-comment
Current x 9.96 ops/sec ±14.16% (24 runs sampled)
New x 11.43 ops/sec ±6.01% (24 runs sampled)
Fastest is New,Current
$ node postcss-comment
Current x 10.07 ops/sec ±14.08% (25 runs sampled)
New x 11.45 ops/sec ±9.09% (26 runs sampled)
Fastest is New
$ node postcss-comment
Current x 10.61 ops/sec ±14.03% (26 runs sampled)
New x 9.65 ops/sec ±12.49% (23 runs sampled)
Fastest is Current
$ node postcss-comment
Current x 10.14 ops/sec ±14.19% (25 runs sampled)
New x 11.91 ops/sec ±10.19% (28 runs sampled)
Fastest is New
$ node postcss-comment
Current x 10.70 ops/sec ±12.20% (25 runs sampled)
New x 10.18 ops/sec ±17.30% (25 runs sampled)
Fastest is Current,New

This ONLY have a small speed different.

@hyperz111
Copy link
Copy Markdown
Contributor Author

hyperz111 commented Dec 31, 2025

Oh, i forgot to say this. NOT all dependencies in that benchmark repo is added in my pnpm store. If i do it, this can be bad.

@ai
Copy link
Copy Markdown
Member

ai commented Dec 31, 2025

Wow! You are hardcore developer.

I will run benchmark, don't worry

@ai ai merged commit 1329142 into postcss:main Dec 31, 2025
10 checks passed
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