sass / embedded-host-node

A Node.js library that will communicate with Embedded Dart Sass using the Embedded Sass protocol
MIT License
165 stars 29 forks source link

Improve launch performance by skipping wrapper script #207

Closed ntkme closed 1 year ago

ntkme commented 1 year ago

This PR ports https://github.com/ntkme/sass-embedded-host-ruby/compare/e14de4a89c8f8d11f42f8cc372370feca81274e7...f08525077596f20655d0cad54d20e168827ce3da, which improves the launch speed of compiler process.

In order to better measure the cost of compiler startup, all the compilations are done sequentially. Test is done on M1 Macbook Pro. Results may vary depends on the platform, but all platforms should see faster launch.

Before:

500 compileStringAsync took 11480.290958046913 milliseconds.
500 compileString took 21916.757833003998 milliseconds.

After:

500 compileStringAsync took 8629.174999952316 milliseconds.
500 compileString took 19083.104707956314 milliseconds.

Benchmark script:

const sass = require('sass-embedded');

async function benchmark (n) {
  const t0 = performance.now();

  for (let i = 0; i < n; i++) {
    await sass.compileStringAsync('a{b:c}');
  }

  const t1 = performance.now();

  console.log(`${n} compileStringAsync took ${t1 - t0} milliseconds.`);

  const t2 = performance.now();

  for (let i = 0; i < n; i++) {
    sass.compileString('a{b:c}');
  }

  const t3 = performance.now();

  console.log(`${n} compileString took ${t3 - t2} milliseconds.`);
}

benchmark(process.argv[2] || 500);