diff options
-rw-r--r-- | README.md | 36 |
1 files changed, 18 insertions, 18 deletions
@@ -1973,12 +1973,26 @@ The interface for `Bun.serve` is based on what [Cloudflare Workers](https://deve ## `Bun.spawn` – spawn a process -`Bun.spawn` lets you quickly spawn a process. +`Bun.spawn` lets you quickly spawn a process. Available as of Bun v0.2.0. -Available as of Bun v0.2.0, it tends to perform around 60% faster than child_process and scales better as your application uses more memory. +Example: + +```ts +import { spawn } from "bun"; + +const { stdout } = spawn(["esbuild"], { + stdin: await fetch( + "https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js" + ), +}); + +const text = await new Response(stdout).text(); +console.log(text); // "const input = "hello world".repeat(400); ..." +``` + +Bun.spawn spawns processes 60% faster than Node.js' `child_process`. ```zig -bun/bench/snippets on main ❯ bun spawn.mjs cpu: Apple M1 Max runtime: bun 0.2.0 (arm64-darwin) @@ -1987,7 +2001,6 @@ benchmark time (avg) (min … max) p75 p99 --------------------------------------------------------- ----------------------------- spawnSync echo hi 888.14 µs/iter (821.83 µs … 1.2 ms) 905.92 µs 1 ms 1.03 ms -bun/bench/snippets on main ❯ node spawn.node.mjs cpu: Apple M1 Max runtime: node v18.9.1 (arm64-darwin) @@ -1997,20 +2010,7 @@ benchmark time (avg) (min … max) p75 p99 spawnSync echo hi 1.47 ms/iter (1.14 ms … 2.64 ms) 1.57 ms 2.37 ms 2.52 ms ``` -```ts -import { spawn } from "bun"; - -const { stdout } = spawn(["esbuild"], { - stdin: await fetch( - "https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js" - ), -}); - -const text = await new Response(stdout).text(); -console.log(text); // "const input = "hello world".repeat(400); ..." -``` - -Synchronous version: +Synchronous example: ```ts import { spawnSync } from "bun"; |