Created
March 27, 2025 15:27
-
-
Save italojs/00cd47fbd09271031c6ec87bc20cb78b to your computer and use it in GitHub Desktop.
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
| /* Results from my local Intel Mac i9 16gb | |
| fs.readFileSync + JSON.parse: | |
| Min: 0.0174 ms | |
| Max: 0.7195 ms | |
| Average: 0.0221 ms | |
| require: | |
| Min: 0.0006 ms | |
| Max: 0.4166 ms | |
| Average: 0.0010 ms | |
| fs.readFile: | |
| Min: 0.0701 ms | |
| Max: 5.3427 ms | |
| Average: 0.1006 ms | |
| Comparison: | |
| 1. require (Avg: 0.0010 ms) | |
| 2. fs.readFileSync + JSON.parse (Avg: 0.0221 ms) | |
| 3. fs.readFile + JSON.parse (Avg: 0.1006 ms) | |
| */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Function to measure performance of fs.readFileSync + JSON.parse | |
| function readWithFsAndParse() { | |
| const start = performance.now(); | |
| const packageJsonPath = path.join(__dirname, 'package.json'); | |
| const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8'); | |
| const packageJson = JSON.parse(packageJsonContent); | |
| const end = performance.now(); | |
| return end - start; | |
| } | |
| // Function to measure performance of require | |
| function readWithRequire() { | |
| const start = performance.now(); | |
| const packageJson = require('./package.json'); | |
| const end = performance.now(); | |
| return end - start; | |
| } | |
| // Function to measure performance of fs.readFile with callback | |
| function readWithFsReadFile() { | |
| return new Promise((resolve) => { | |
| const start = performance.now(); | |
| const packageJsonPath = path.join(__dirname, 'package.json'); | |
| fs.readFile(packageJsonPath, 'utf8', (err, data) => { | |
| if (err) throw err; | |
| const packageJson = JSON.parse(data); | |
| const end = performance.now(); | |
| resolve(end - start); | |
| }); | |
| }); | |
| } | |
| // Benchmark function | |
| async function runBenchmark(iterations = 10000) { | |
| console.log(`Running benchmark with ${iterations} iterations:`); | |
| // Warm-up runs | |
| for (let i = 0; i < 100; i++) { | |
| readWithFsAndParse(); | |
| readWithRequire(); | |
| await readWithFsReadFile(); | |
| } | |
| // Benchmark fs.readFileSync + JSON.parse | |
| const fsParseTimings = []; | |
| for (let i = 0; i < iterations; i++) { | |
| fsParseTimings.push(readWithFsAndParse()); | |
| } | |
| // Benchmark require | |
| const requireTimings = []; | |
| for (let i = 0; i < iterations; i++) { | |
| requireTimings.push(readWithRequire()); | |
| } | |
| // Benchmark fs.readFile | |
| const fsReadFileTimings = []; | |
| for (let i = 0; i < iterations; i++) { | |
| fsReadFileTimings.push(await readWithFsReadFile()); | |
| } | |
| // Calculate statistics | |
| const calculateStats = (timings) => ({ | |
| min: Math.min(...timings), | |
| max: Math.max(...timings), | |
| average: timings.reduce((a, b) => a + b, 0) / timings.length | |
| }); | |
| const fsParseStats = calculateStats(fsParseTimings); | |
| const requireStats = calculateStats(requireTimings); | |
| const fsReadFileStats = calculateStats(fsReadFileTimings); | |
| // Print results | |
| console.log('\nfs.readFileSync + JSON.parse:'); | |
| console.log(` Min: ${fsParseStats.min.toFixed(4)} ms`); | |
| console.log(` Max: ${fsParseStats.max.toFixed(4)} ms`); | |
| console.log(` Average: ${fsParseStats.average.toFixed(4)} ms`); | |
| console.log('\nrequire:'); | |
| console.log(` Min: ${requireStats.min.toFixed(4)} ms`); | |
| console.log(` Max: ${requireStats.max.toFixed(4)} ms`); | |
| console.log(` Average: ${requireStats.average.toFixed(4)} ms`); | |
| console.log('\nfs.readFile:'); | |
| console.log(` Min: ${fsReadFileStats.min.toFixed(4)} ms`); | |
| console.log(` Max: ${fsReadFileStats.max.toFixed(4)} ms`); | |
| console.log(` Average: ${fsReadFileStats.average.toFixed(4)} ms`); | |
| console.log('\nComparison:'); | |
| const methods = [ | |
| { name: 'fs.readFileSync + JSON.parse', stats: fsParseStats }, | |
| { name: 'require', stats: requireStats }, | |
| { name: 'fs.readFile', stats: fsReadFileStats } | |
| ]; | |
| methods.sort((a, b) => a.stats.average - b.stats.average); | |
| methods.forEach((method, index) => { | |
| console.log(` ${index + 1}. ${method.name} (Avg: ${method.stats.average.toFixed(4)} ms)`); | |
| }); | |
| } | |
| // Run the benchmark | |
| runBenchmark(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment