Last active
April 9, 2021 08:08
-
-
Save fson/07740bd5aa7d331963ae0f16b857efcf to your computer and use it in GitHub Desktop.
spawn-benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const spawnAsync = require("@expo/spawn-async"); | |
const assert = require("assert"); | |
const childProcess = require("child_process"); | |
const crossSpawn = require("cross-spawn"); | |
function profileExecSync() { | |
console.time("execSync"); | |
const output = childProcess.execSync("xcodebuild -version").toString(); | |
console.timeEnd("execSync"); | |
assert(output.includes("Build version")); | |
} | |
function profileExecFileSync() { | |
console.time("execFileSync"); | |
const output = childProcess | |
.execFileSync("xcodebuild", ["-version"]) | |
.toString(); | |
console.timeEnd("execFileSync"); | |
assert(output.includes("Build version")); | |
} | |
function profileSpawnSync() { | |
console.time("spawnSync"); | |
const output = childProcess | |
.spawnSync("xcodebuild", ["-version"]) | |
.stdout.toString(); | |
console.timeEnd("spawnSync"); | |
assert(output.includes("Build version")); | |
} | |
function profileCrossSpawn() { | |
return new Promise((resolve, reject) => { | |
console.time("cross-spawn"); | |
let output = ""; | |
const child = crossSpawn | |
.spawn("xcodebuild", ["-version"]) | |
.once("close", () => { | |
console.timeEnd("cross-spawn"); | |
if (output.includes("Build version")) { | |
resolve(); | |
} else { | |
reject(new Error("invalid output")); | |
} | |
}); | |
child.stdout.on("data", (buffer) => { | |
output += buffer.toString(); | |
}); | |
}); | |
} | |
async function profileSpawnAsync() { | |
console.time("@expo/spawn-async"); | |
const output = (await spawnAsync("xcodebuild", ["-version"])).stdout; | |
console.timeEnd("@expo/spawn-async"); | |
assert(output.includes("Build version")); | |
} | |
async function profile() { | |
for (let i = 0; i < 10; i++) { | |
profileExecSync(); | |
} | |
for (let i = 0; i < 10; i++) { | |
profileExecFileSync(); | |
} | |
for (let i = 0; i < 10; i++) { | |
profileSpawnSync(); | |
} | |
for (let i = 0; i < 10; i++) { | |
await profileCrossSpawn(); | |
} | |
for (let i = 0; i < 10; i++) { | |
await profileSpawnAsync(); | |
} | |
} | |
profile().catch((error) => console.error(error)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "spawn-benchmark", | |
"version": "1.0.0", | |
"bin": "benchmark.js", | |
"license": "MIT", | |
"dependencies": { | |
"@expo/spawn-async": "^1.5.0", | |
"cross-spawn": "^6.0.5" | |
}, | |
"devDependencies": { | |
"prettier": "^2.2.1" | |
}, | |
"prettier": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment