Skip to content

Commit

Permalink
draft: some exploration about performance of execSync
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 18, 2023
1 parent 5a58972 commit 856245f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const {
ChildProcess,
stdioStringToArray,
} = child_process;
const perf_hooks = require('perf_hooks');

const MAX_BUFFER = 1024 * 1024;

Expand Down Expand Up @@ -825,10 +826,13 @@ function spawn(file, args, options) {
* }}
*/
function spawnSync(file, args, options) {
let now = perf_hooks.performance.now();
options = {
maxBuffer: MAX_BUFFER,
...normalizeSpawnArguments(file, args, options),
};
console.log(`execSync spawnSync > normalizeSpawnArguments: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

debug('spawnSync', options);

Expand All @@ -838,9 +842,15 @@ function spawnSync(file, args, options) {
// Validate maxBuffer, if present.
validateMaxBuffer(options.maxBuffer);

console.log(`execSync spawnSync > validation: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

// Validate and translate the kill signal, if present.
options.killSignal = sanitizeKillSignal(options.killSignal);

console.log(`execSync spawnSync > sanitizeKillSignal: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

options.stdio = getValidStdio(options.stdio || 'pipe', true).stdio;

if (options.input) {
Expand Down Expand Up @@ -868,7 +878,12 @@ function spawnSync(file, args, options) {
}
}

return child_process.spawnSync(options);
console.log(`execSync spawnSync > stdio & input: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();
const result = child_process.spawnSync(options);
console.log(`execSync spawnSync > child_process.spawnSync: ${perf_hooks.performance.now() - now}`);

return result;
}


Expand Down Expand Up @@ -946,16 +961,27 @@ function execFileSync(file, args, options) {
* @returns {Buffer | string}
*/
function execSync(command, options) {
let now = perf_hooks.performance.now();
const opts = normalizeExecArgs(command, options, null);
console.log(`execSync normalizeExecArgs: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();
const inheritStderr = !opts.options.stdio;

const ret = spawnSync(opts.file, opts.options);
console.log(`execSync spawnSync: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

if (inheritStderr && ret.stderr)
process.stderr.write(ret.stderr);

console.log(`execSync inheritStderr: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

const err = checkExecSyncError(ret, undefined, command);

console.log(`execSync checkExecSyncError: ${perf_hooks.performance.now() - now}`);
now = perf_hooks.performance.now();

if (err)
throw err;

Expand Down

0 comments on commit 856245f

Please sign in to comment.