乐闻世界logo
搜索文章和话题

What are pnpm's performance advantages and how does it compare to npm/Yarn?

3月6日 21:34

pnpm's performance advantages are mainly reflected in installation speed, disk space, and memory usage.

Installation Speed Comparison:

bash
# Test scenario: Installing project with 1000+ dependencies # First install (cold start) npm install: 45s yarn install: 38s pnpm install: 25s # Second install (with cache) npm install: 15s yarn install: 8s pnpm install: 2s # Hard links, almost instant # Reinstall after deleting node_modules npm install: 40s yarn install: 35s pnpm install: 3s # Create hard links from store

Disk Space Saving:

bash
# 10 projects with same tech stack # npm approach Each project node_modules: ~500MB Total usage: 500MB × 10 = 5GB # pnpm approach Global store: ~500MB Each project hard links: ~few KB (metadata) Total usage: ~500MB # Space saved: 90%

Performance Advantage Principles:

  1. Hard Link Mechanism
javascript
// npm/yarn approach // Each project copies complete files project-a/node_modules/lodash.js // 1.4MB project-b/node_modules/lodash.js // 1.4MB project-c/node_modules/lodash.js // 1.4MB // Total: 4.2MB // pnpm approach // All projects share same physical file ~/.pnpm-store/lodash.js // 1.4MB project-a/node_modules/lodash.js // Hard link project-b/node_modules/lodash.js // Hard link project-c/node_modules/lodash.js // Hard link // Total: 1.4MB
  1. Content-Addressable Storage
javascript
// Files with same content stored only once // Even if in different packages const hash = getContentHash(fileContent); // Storage path: ~/.pnpm-store/files/${hash}
  1. Parallel Downloads
bash
# pnpm downloads dependencies in parallel by default # npm downloads serially by default # .npmrc configuration network-concurrency=16 # Concurrent download count

Memory Usage Advantages:

bash
# pnpm's node_modules structure is more compact # File system cache is more effective # Memory usage comparison (large project) npm: ~800MB yarn: ~750MB pnpm: ~400MB

Performance Benchmark:

javascript
// Test using benchmark const { execSync } = require('child_process'); // Test installation time console.time('npm'); execSync('npm install'); console.timeEnd('npm'); // ~45s console.time('pnpm'); execSync('pnpm install'); console.timeEnd('pnpm'); // ~25s

Real Project Examples:

bash
# Vue 3 project (~1500 dependencies) npm install: 52s, 1.2GB disk yarn install: 45s, 1.1GB disk pnpm install: 28s, 450MB disk # React project (~800 dependencies) npm install: 35s, 800MB disk yarn install: 30s, 750MB disk pnpm install: 18s, 280MB disk

Performance Optimization Configuration:

toml
# .npmrc # Increase concurrency network-concurrency=16 # Use local cache prefer-frozen-lockfile=true # Disable progress bar (slightly improves performance) reporter=silent

Performance Comparison Summary:

MetricnpmYarnpnpm
First InstallSlowMediumFast
Second InstallMediumFastFastest
Disk UsageHighHighLow
Memory UsageHighMediumLow
Parallel Download
Hard Links

Applicable Scenarios:

  • ✅ Multi-project development environment
  • ✅ CI/CD pipelines
  • ✅ Monorepo projects
  • ✅ Disk space constrained environments
标签:PNPM