Skip to content

Instantly share code, notes, and snippets.

@JanMiksovsky
Created June 27, 2024 21:46
Show Gist options
  • Save JanMiksovsky/25770fd76cf350ff01d746b040690285 to your computer and use it in GitHub Desktop.
Save JanMiksovsky/25770fd76cf350ff01d746b040690285 to your computer and use it in GitHub Desktop.
Measure difference in performance of writeFile(string value) vs writeFile(String object)
import fs from "node:fs/promises";
// Create a 1MB string value to a file
const string = "a".repeat(1000000);
const startValue = performance.mark("string");
await fs.writeFile("1MB.txt", string);
const measureValue = performance.measure("write string value", startValue);
console.log(`${measureValue.name}: ${measureValue.duration}ms`);
// Write the string as a String object
const object = new String(string);
const startObject = performance.mark("String");
await fs.writeFile("1MBString.txt", object);
const measureObject = performance.measure("write String object", startObject);
console.log(`${measureObject.name}: ${measureObject.duration}ms`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment